对于使用jasmine
作为框架和karma
作为测试运行符的服务编写适当的单元测试,我几乎没有问题。
这是我在 example-service.js 中实现的:
export default class ExampleService {
constructor($resource, $http) {
'ngInject';
this.$resource = $resource;
this.$http = $http;
}
exampleMethodOne() {
//some code lines
}
exampleMethodTwo() {
//some code lines
}
}
ExampleService.selector = 'myExampleService';
这是我在测试 example-service.test.js
中写的内容let myExampleService, $httpBackend;
beforeEach(() => {
angular
.module('exampleApp', [])
.service(ExampleService.selector, ExampleService);
angular.mock.module('exampleApp');
});
beforeEach(inject((_myExampleService_, _$httpBackend_) => {
myExampleService = _myExampleService_;
$httpBackend = _$httpBackend_;
}));
我已导入angular-mocks.js
和example-service.js
当我尝试这种情况时,控制台会抛出Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- myExampleService
错误。
请帮我解决这个问题。
答案 0 :(得分:0)
$resource
需要加载angular-resource.js和相应的模块:
beforeEach(() => {
angular
.module('exampleApp', ['ngResource'])
.service(ExampleService.selector, ExampleService);
angular.mock.module('exampleApp');
});