I'm new to unit testing with jasmine
, and I've got some issues while trying to make it work. I'm using a project generated with yeoman
, using gulp-angular
generator.
I have a service :
export class AuthService {
constructor (FactoryAppStateService) {
'ngInject';
this.FactoryAppStateService = FactoryAppStateService;
this.test = 'toto';
}
login (loginInfos) {
var user = {
'userName' : loginInfos.username,
'token' : 'token'
}
var credentials = {
'cientId' : 'testFromService',
'clientSecret' : 'testFromService'
}
this.FactoryAppStateService.setUser(user);
this.FactoryAppStateService.setClientId(credentials.clientId);
this.FactoryAppStateService.setClientSecret(credentials.clientSecret);
this.test = 'tata';
return user.token;
}
getTest () {
return this.test;
}
}
and a test :
describe('service Auth', () => {
// load the service's module
beforeEach(angular.mock.module('monNotaireFront'));
it('should do something', function () {
var service;
angular.mock.inject(function GetDependencies(AuthService) {
service = AuthService;
});
expect(service.getTest()).toBe('tata');
});
});
And I'm getting this Error :
PhantomJS 1.9.8 (Windows 7 0.0.0) service Auth should do something FAILED Error: [$injector:unpr] Unknown provider: FactoryAppStateServiceProvider <- FactoryAppStateService <- AuthService http://errors.angularjs.org/1.4.7/ $injector/unpr?p0=FactoryAppStateServiceProvider%20%3C-%20FactoryAppStateService%20%3C-%20AuthService at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4289 at getService (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4294 at getService (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) at invoke (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4469) at instantiate (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4486) at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bo wer_components/angular/angular.js:4346 at invoke (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4478) at enforcedReturnValue (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4330) at invoke (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4478) at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4295 at getService (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) at invoke (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4469) at workFn (C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular-mocks/angular-mocks.js:2438) at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular-mocks/angular-mocks.js:2410 at C:/Users/gayard/Documents/Dev/monnotaire-angularjs/.tmp/serve/app/index.module.js:9
And I have no idea why the factory used by my service can't be injected. I was trying different solutions like this one before :
describe('service Auth', () => {
beforeEach(angular.mock.module('monNotaireFront'));
let AuthService;
beforeEach(inject((_AuthService_) => {
AuthService = _AuthService_;
}));
it("should return tata", () => {
AuthService.login({username : 'test'});
expect(AuthService.getTest()).toEqual('tata');
});
});
But same error occurred .
Please explain what could be the reason?
Problem Solved ! It was a bad declaration of FactoryAppStateService. To be simple, instead of
angular.module([]).factory('FactoryAppState', (localStorageService) => {return new FactoryAppState(localStorageService)})
i was doing
angular.module([]).factory('FactoryAppState', FactoryAppState)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.