I am trying to begin writing unit tests for my angular application and hit a stopping block pretty quick as I am unsure of how exactly to mock my service in a testable way. My service is as follows: var services = angular.module('myApp', ['ngResource']);
services.factory('LogonFactory', function ($resource, $http) {
return $resource('/servicemen/logon/', {}, {
login: {
method: 'POST'
}
})
});
My test consists thus far of:
describe('Logon', function () {
var mockUserResource, $httpBackend;
beforeEach(angular.mock.module('myApp'));
beforeEach(function () {
angular.mock.inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
mockLoginResource = $injector.get('login');
})
});
describe('login', function () {
it('should call login function', inject(function (User) {
$httpBackend.expectGET('/servicemen/logon/')
.respond([{
username: 'test'
}]);
var result = mockLoginResource.login();
$httpBackend.flush();
expect(result[0].username).toEqual('test');
}));
});
});
Also I am getting error No flush !!! Thanks in advance.