为服务编写一个单元测试:AngularJS



对于使用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';

这是我在我的测试示例-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.jsexample-service.js

当我尝试这种情况时,控制台将抛出Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- myExampleService 错误。

请帮我解决这个问题。

$resource需要角度资源.js和相应的模块来加载:

beforeEach(() => {
angular
.module('exampleApp', ['ngResource'])
.service(ExampleService.selector, ExampleService);
angular.mock.module('exampleApp');
});

相关内容

最新更新