我是单元测试的新手jasmine
,我在尝试使其工作时遇到了一些问题。我正在使用yeoman
生成的项目,使用gulp-angular
生成器。
我有服务:
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;
}
}
和测试:
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');
});
});
我收到此错误:
PhantomJS 1.9.8(Windows 7 0.0.0)服务Auth应该做点什么 失败 错误:[$ injector:unpr]未知提供者:FactoryAppStateServiceProvider< - FactoryAppStateService< - AuthService http://errors.angularjs.org/1.4.7/ $注射器/ unpr?P0 = FactoryAppStateServiceProvider%20%3 C-%20FactoryAppStateService%20%3 C-%20AuthService 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4289 在getService(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4294 在getService(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) 在调用时(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4469) 在实例化(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4486) 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4346 在调用时(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) 在调用时(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4478) 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4295 在getService(C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular/angular.js:4437) 在调用时(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) 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/bower_components/angular-mocks/angular-mocks.js:2410 在C:/Users/gayard/Documents/Dev/monnotaire-angularjs/.tmp/serve/app/index.module.js:9
我不知道为什么我的服务所使用的工厂无法注入。 我之前尝试过不同的解决方案:
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');
});
});
但发生了同样的错误。
请解释可能是什么原因?