Reputation: 399
This is my Controller:
rcCtrls.controller('LoginController', [
'$scope',
'AuthService',
function($scope, AuthService)
{
console.log('LoginController');
$scope.credantials = {
email: '',
password: '',
remember: false
}
$scope.login = function(credantials)
{
console.log('before login');
AuthService.login(credantials.email, credantials.password, credantials.remember || false).success(function(response) {
console.log('login successful');
console.log(response);
location.reload();
}).error(function() {
console.log('failed to login');
});
};
}]);
This is my AuthService:
rcServices.factory('AuthService', [
'$http',
function($http)
{
return {
login: function(email, password, remember) {
console.log('Auth Service Login');
return $http.post('/auth/login', {email: email, password: password, remember: remember});
},
logout: function() {
return $http.post('/auth/logout');
}
};
}]);
This is my spec:
describe('Controller: LoginController', function() {
// We initiate the app moudle mock
beforeEach(module('app')); // alias for angular.mock.module('app')
beforeEach(inject(function($controller, $rootScope, AuthService, $httpBackend){
this.$httpBackend = $httpBackend;
this.scope = $rootScope.$new();
$controller('LoginController', {
$scope: this.scope,
AuthService: AuthService
});
}));
describe("checking credantials at the begining", function() {
it("should be initiated", function() {
expect(this.scope.credantials).toEqual({ email: '', password: '', remember: false });
});
});
describe("successfully logged in", function() {
it("should redirect to home", function() {
this.scope.credantials = { email: '[email protected]', password: '123', remember: false };
this.$httpBackend.expectPOST('/auth/login', this.scope.credantials).respond(200, {test: 'test'});
this.scope.login(this.scope.credantials);
console.log(this);
this.$httpBackend.flush();
expect(this.scope).toBeDefined();
});
});
});
It seems as if the $httpBackend doesn't do what it is suppose to do.
I indeed get all the console logs up until the moment where my Service uses $http post request, and there it stops.
FYI, my controller is working properly on the app itself! The post is happening and the response is returning properly.
However the test just doesn't...
I'm getting and error:
Error: No pending request to flush !
Commenting it doesn't help.
EDIT:
The issue was my angular-mock.js version.... how sad.
Upvotes: 0
Views: 368
Reputation: 4302
In your service you call:
location.reload();
This could be messing with jasmine and causing unpredictable behavior. What does this call do, do you need it, and what happens to your tests if you just comment that out?
If you really need to make that call, you should use AngularJS built in $location service and mock it so that you keep the behavior but not break jasmine in the process.
===========
Update:
Additionally, it's a little odd that you're using this.httpBackend, when normally you'd totally overwrite the reference - not storing it as an attribute of this but a variable global to your tests. I am suspicious that somewhere else in your app you have called flush already and the reference isn't being reset properly. Do you have any other tests calling flush, anywhere?
http://plnkr.co/edit/jBewavvO1n8NBa6WGFja
I made working tests in this plunkr, which pass with your code as it is. With the exception of the location.reload() line which I commented out, which did not break the tests, although this test runner is a bit different as its in a plunkr and that won't always be the case. You really should not be calling that in tests.
Upvotes: 0