我正在尝试在.spec.js文件上注入服务。我正在使用AngularJS和Karma进行单元测试。这是我的代码:
test.spec.js:
import TestModule from './test';
import TestController from './test.controller';
import TestComponent from './test.component';
import TestTemplate from './test.html';
import LoginService from '../../login/login.service';
describe('Test', () => {
let $rootScope, makeController;
beforeEach(window.module(TestModule.name));
beforeEach(
inject((_$rootScope_, LoginService) => {
$rootScope = _$rootScope_;
LoginService = _LoginService_;
makeController = () => {
return new TestController(LoginService);
};
}));
describe('Controller', () => {
it('setDescription', () => {
let controller = makeController();
expect(controller.setDescription(10)).to.equal('child');
})
});
});
test.controller.js
class TestController {
constructor($scope, LoginService) {
'ngInject';
LoginService.getDataLabels('test', (err,data) => {
this.dataLabels = data;
});
}
setDescription(value) {
return 'child';
}
}
login.service.js
import $ from 'jquery';
let LoginService = function () {
'ngInject';
let getDataLabels = (view, cb) => {
return 'test';
};
return {
getDataLabels
};
};
export default LoginService;
当我尝试运行 karma start 时,出现以下错误:
“之前”钩子:“ setDescription”的WorkFn 错误:[$ injector:unpr]未知提供程序:LoginServiceProvider <-LoginService
似乎我无法在控制器上测试 setDescription 功能。
如何在规范文件中正确插入 LoginService ?我在做什么错了?
谢谢。