单元测试服务:AngularJS(Unit test a service : AngularJS)

2019-09-28 18:44发布

我有写一个合适的单元测试用于使用服务的几个问题jasmine作为框架和karma的测试运行。

以下是我在例如-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';

下面是我在我的测试例子,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错误。

请帮我解决这个问题。

文章来源: Unit test a service : AngularJS