I have created an angular service which I am testing without any issue, I then started trying to inject a dependency into the tests when I started having an issue.
I want to make sure that a function in the dependency has been called but it's coming back undefined.
Here's my service:
angular.module('enigmaApp.services', [])
.factory('auth', function($window, $http, SS){
var auth = {};
auth.register = function(user){
return $http.post('/register', user)
.success(function(data){
if(data.token){
SS.setObj('appToken', data.token);
}
});
};
});
My test:
describe('Auth Service Tests', function () {
var $httpBackend, auth, defer, registerReqHandler, setObjSpy, SS, SSMock, user;
beforeEach(module('enigmaApp'));
beforeEach(function () {
// Create spies
setObjSpy = jasmine.createSpy('setObj');
SSMock = {
setObj: setObjSpy
}
SS = SSMock;
module(function ($provide) {
$provide.value('SS', SSMock);
});
});
beforeEach(inject(function (_$httpBackend_, $injector, $q) {
$httpBackend = _$httpBackend_;
defer = $q.defer();
registerReqHandler = $httpBackend.when('POST', '/register').respond(defer.promise);
auth = $injector.get('auth');
}));
afterEach(function () {
$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('auth.register(user)', function () {
beforeEach(function () {
user = {
email: 'bwayne@wayneenterprise.com',
first_name: 'Bruce',
last_name: 'Wyane',
password: 'password123'
};
});
it('should call SS.setObj on successful registration', function () {
$httpBackend.expectPOST('/register').respond({ token: 'fakeToken' });
auth.register(user);
expect(SS.setObj).toHaveBeenCalled();
});
});
When I run the tests I get a failed test that says "Expected spy setObj to have been called." Any idea on what I'm doing wrong here? I thought I set up a mock for SS.setObj and provided it to the module,
Aucun commentaire:
Enregistrer un commentaire