茉莉花测试:错误:[$ injector:unpr]未知提供者:

时间:2016-09-15 18:16:46

标签: angularjs jasmine karma-jasmine

我有以下代码:

angular.module('ps.workspaces').config(['$provide', function($provide) {
  $provide.factory('workspacesNonCompliance', ['$filter', function($filter) {
    return {
      NonComplianceReasons: [
        {name: $filter('translate')('NONCOMPLIANT.ALL'), noncompliant_properties: '' },
        {name: $filter('translate')('NONCOMPLIANT.JAIL_BREAK_DETECTION'), noncompliant_properties: 'jail_break_detection' },
        {name: $filter('translate')('NONCOMPLIANT.COMPLIANCE_ROOTED_DETECTION'), noncompliant_properties: 'compliance_rooted_detection' },
        {name: $filter('translate')('NONCOMPLIANT.COMPLIANCE_USB_DEBUGGING'), noncompliant_properties: 'compliance_usb_debugging'},
        {name: $filter('translate')('NONCOMPLIANT.POLICY_EXPIRED'), noncompliant_properties: 'policy_expired'}
      ]
    };
  }]);
}]);

我的模块是:

angular.module('ps.workspaces', [
  'ps.workspaces.workspacesDirective',
  'ps.workspaces.workspacesFactories'
]);

我的测试是:

describe('Tests for workspacesNonCompliance /', function() {
  var workspacesNonCompliance,
      $provider,
      $filter;

  // beforeEach(module('ps.workspaces'));
  // beforeEach(module('ps.workspaces.workspacesDirective'));
  // beforeEach(module('ps.workspaces.workspacesFactories'));

  beforeEach(function(){
    angular.module('ps.workspaces', [
      'ps.workspaces.workspacesDirective',
      'ps.workspaces.workspacesFactories'
    ]);
 });

  describe('workspacesNonCompliance is array of noncompliance reasons /', function () {
    beforeEach(function () {
      inject(function ($injector) {
        workspacesNonCompliance = $injector.get('workspacesNonCompliance');
        $filter = $injector.get('$filter');

      });
    });

    it('workspacesNonCompliance has array of noncompliance reasons', function () {
      console.log('*******  workspacesNonCompliance ', workspacesNonCompliance);
      expect(workspacesNonCompliance.NonComplianceReasons).toContain({name: $filter('translate')('NONCOMPLIANT.ALL'), noncompliant_properties: '' });
    });

  });
});

跑完测试并收到错误消息:

错误:[$ injector:unpr]未知提供者:workspacesNonComplianceProvider< - workspacesNonCompliance http://errors.angularjs.org/1.3.18/ $注射器/ unpr?P0 = workspacesNonComplianceProvider%20%3 C-%20workspacesNonCompliance     at /Users/joannepham/git/pws/console-ui/bower_components/angular/angular.js:63:12     at /Users/joannepham/git/pws/console-ui/bower_components/angular/angular.js:4031:19

无法弄清楚在这种情况下需要解决的问题。请帮忙

1 个答案:

答案 0 :(得分:0)

不确定ps.workspaces.workspacesDirectiveps.workspaces.workspacesFactories有哪些内容。但这里的主要问题是,workspacesNonCompliance未正确注入。这就是为什么它以undefined出现,因此出现错误。

您的测试文件应该是:

describe('Tests for workspacesNonCompliance /', function() {
  var workspacesNonCompliance, $provider, $filter;

  beforeEach(function() {
    module('ps.workspaces');
  });

  beforeEach(inject(function(_workspacesNonCompliance_) {
    workspacesNonCompliance = _workspacesNonCompliance_;
  }));

  describe('workspacesNonCompliance', function() {
    it('should have workspacesNonCompliance as defined', function() {
      expect(workspacesNonCompliance).toBeDefined();
    });
  });
});

希望这有帮助。