Unit test a service : AngularJS

吃可爱长大的小学妹 提交于 2019-12-11 06:01:58

问题


I have few questions with to write a proper unit test for a service using jasmine as framework and karma as test runner.

here is what i implemented in 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';

Here what i wrote in my test 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_;
          })); 

i have imported the angular-mocks.js, angular-resource.js and example-service.js

when i try this scenario the console will throw a Error: [$injector:unpr] Unknown provider: $urlMatcherFactoryProvider <- $urlMatcherFactory <- myExampleService error.

please help me to solve this.

来源:https://stackoverflow.com/questions/50624650/unit-test-a-service-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!