单元测试服务:AngularJS

时间:2018-05-31 12:44:21

标签: javascript angularjs ecmascript-6 karma-jasmine angularjs-service

对于使用jasmine作为框架和karma作为测试运行符的服务编写适当的单元测试,我几乎没有问题。

这是我在 example-service.js 中实现的:

export default class ExampleService {
constructor($resource, $http, $urlMatcherFactory) {
'ngInject';

this.$resource = $resource;
this.$http = $http;
this.$urlMatcherFactory = $urlMatcherFactory;
}

exampleMethodOne() {
//some code lines
}

exampleMethodTwo() {
//some code lines 
}

}
ExampleService.selector = 'myExampleService';

这是我在测试 example-service.test.js

中写的内容
        let myExampleService, $httpBackend, $urlMatcherFactory;

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

          beforeEach(inject((_myExampleService_, _$httpBackend_, 
_$urlMatcherFactory_) => {
            myExampleService = _myExampleService_;
            $httpBackend = _$httpBackend_;
            $urlMatcherFactory = _$urlMatcherFactory_;
          })); 

我导入了angular-mocks.jsangular-resource.jsexample-service.js

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

请帮我解决这个问题。

0 个答案:

没有答案
相关问题