Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 服务的AngularJS单元测试:$urlMatcherFactory引发未知错误_Javascript_Angularjs_Ecmascript 6_Angularjs Service - Fatal编程技术网

Javascript 服务的AngularJS单元测试:$urlMatcherFactory引发未知错误

Javascript 服务的AngularJS单元测试:$urlMatcherFactory引发未知错误,javascript,angularjs,ecmascript-6,angularjs-service,Javascript,Angularjs,Ecmascript 6,Angularjs Service,我对如何使用jasmine作为框架和karma作为测试运行者为服务编写适当的单元测试没有什么疑问 下面是我在示例服务.js中实现的内容: export default class ExampleService { constructor($resource, $http, $urlMatcherFactory) { 'ngInject'; this.$resource = $resource; this.$http = $http; this.$urlMatcherFactory = $url

我对如何使用
jasmine
作为框架和
karma
作为测试运行者为服务编写适当的单元测试没有什么疑问

下面是我在示例服务.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';
下面是我在test示例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.js
angular resource.js
example service.js


当我尝试此方案时,控制台将抛出一个
错误:[$injector:unpr]未知提供程序:$urlmacherfactoryprovider我想
$urlmacherfactory
引用了,并且UI路由器未加载

不建议在单元测试中使用真正的路由器,因为它提供了额外的活动部件并打破了单元之间的隔离。根据经验,除了经过测试的单元外,每个单元都应该被模仿

$URLMacherFactory
必须配置存根方法以返回预期值。由于它们在
ExampleService
构造期间使用,因此应在服务实例化之前配置它们:

  let removeAllUrlMock;

  beforeEach(() => {
    removeAllUrlMock = jasmine.createSpyObj('removeAllUrl', ['format']);
    $urlMatcherFactory = jasmine.createSpyObj('urlMatcherFactory', ['compile']);
    $urlMatcherFactory.compile.and.returnValue(removeAllUrlMock);

    angular
      .module('exampleApp', ['ngResource'])
      .service(ExampleService.selector, ExampleService);
    angular.mock.module('exampleApp');
    angular.mock.module({ $urlMatcherFactory });
  });
这可以通过以下方式进行测试:

expect($urlMatcherFactory.compile).toHaveBeenCalledOnce();
expect($urlMatcherFactory.compile).toHaveBeenCalledWith('../api/v1/user/{Id}/remove/all');
expect(myExampleService.removeAllUrl).toBe(removeAllUrlMock);
然后在使用时可以模拟和测试特定的
removeAllUrl
调用:

removeAllUrlMock.format.and.returnValue('../api/v1/user/foo/remove/all');
myExampleService.removeProduct('foo');
expect(removeAllUrlMock.format).toHaveBeenCalledOnce();
expect($urlMatcherFactory.compile).toHaveBeenCalledWith(
  jasmine.objectContaining({ id: 'foo' })
);
由于
$urlMatcherFactory
是一种实用服务,不提供太多的活动部件,而且应该是可预测的,因此也可以直接导入和使用,而无需UI路由器模块:

import { UrlMatcherFactory } from '@uirouter/angularjs';

...

  beforeEach(() => {
    angular
      .module('exampleApp', ['ngResource'])
      .service(ExampleService.selector, ExampleService);
    angular.mock.module('exampleApp');
    angular.mock.module(($provide) => {
      $provide.service('$urlMatcherFactory', UrlMatcherFactory });
    });
  });
那就只能被监视了:

spyOn(myExampleService, 'removeAllUrl').and.callThrough();
myExampleService.removeProduct('foo');
expect(removeAllUrlMock.format).toHaveBeenCalledOnce();
expect($urlMatcherFactory.compile).toHaveBeenCalledWith(
  jasmine.objectContaining({ id: 'foo' })
);

考虑一下下面的场景,它应该如何调用?因为它得到了未定义的'removeProduct(id){返回这个。$http({method:'DELETE',url:this.removeAllUrl.format({id:id})});}`什么是removeAllUrl?问题不包含您正在测试的代码。this.removeAllUrl=this.$urlMatcherFactory.compile(“../api/v1/user/{Id}/remove/all”);它在构造函数中初始化,并更新了答案。希望描述足够清楚。您是否将包含$urlMatchFactory的uirouter添加到Karma中的文件数组中?angular-ui-router.min.js可能是文件名。为什么要逐字重复?