使用Angular Mocks测试Angular:Provider'$$ sanitizeUri'必须定义$ get factory方法

时间:2016-04-23 10:03:17

标签: angularjs unit-testing

我正在使用Angular Mocks测试我的应用程序,当它尝试创建一些模块时,我遇到了这个问题。

describe('navRegisterOverrideSpec', function () {
  var navigationServiceProvider;

  beforeEach(module('wrapper'));

  describe('remove un needed navs', function () {
    var navigationService;

    beforeEach(
      inject(function ($injector) {
        navigationService = $injector.get('navigationService');
      }));

    it('Should not have admin menu', function () {
      var filtered = _.filter(navigationService.items, function (item) {
        return item.id == 'admin_menu';
      });

      expect(filtered.length).toEqual(0);
    });
  });
});

以下是我得到的错误。

  

Error: [$injector:modulerr] Failed to instantiate module ng due to:
Error: [$injector:pget] Provider '$$sanitizeUri' must define $get factory method.
http://errors.angularjs.org/1.5.5/$injector/pget?p0=%24%24sanitizeUri
    at provider (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4497)
    at D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:366
    at forEach (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:336)
    at D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4484
    at ngModule (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:2530)
    at invoke (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4665)
    at runInvokeQueue (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4558)
    at D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4567
    at forEach (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:322)
    at loadModules (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4590)
    at createInjector (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular/angular.js:4470)
    at workFn (D:/Projects/l35workflow/src/portals/verybest/bower_components/angular-mocks/angular-mocks.js:2464)
http://errors.angularjs.org/1.5.5/$injector/modulerr?p0=ng&p1=Error%3A%20%5B%24injector%3Apget%5D%20Provider%20'%

我找不到任何答案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,偶然发现了this github问题,然后引发了我linked github问题。我不知道你是否使用业力作为你的无头网络浏览器的测试运行和幻像,但更新我的karma-phantomjs-launcher^1.0.0最终解决了我的问题。

问题的原因(对于那些不想通过我上面提到的链接的人),PhantomJS没有得到Angular的正式支持,反过来karma-phantomjs-launcher的旧版本是使用处理new / bind的旧Web引擎与Angular使用的不同。