与yeoman + angularjs + karma测试的未知提供者

时间:2014-06-12 09:58:08

标签: angularjs karma-runner

我是角度测试的新手,我已经遇到了问题。

我通过

创建了一项服务
yo angular:factory Flash

一旦填写就好像

'use strict';

angular.module('ociWebPosApp')
  .factory('Flash', [
    '$rootScope',
    '$timeout',
    function($rootScope, $timeout) {
      var secs = 3,
        lastFlash,

        clear = function() {
          $timeout.cancel(lastFlash);
          $rootScope.flash = {};
        },

        show = function(message) {
          $timeout.cancel(lastFlash);
          $rootScope.flash = message;
          lastFlash = $timeout(clear, secs * 1000);
        };

      return {
        show: function(message) {
          show(message);
        },
        clear: function() {
          clear();
        },
        setLoading: function() {
          $rootScope.flash = {
            message: 'LOADING',
            type: 'loading'
          };
        },
        setApiError: function() {
          show({
            message: 'API_ERROR',
            type: 'warning'
          });
        }
      };
    }
  ]);

测试文件看起来像

'use strict';

describe('Service: Flash', function() {

  // load the service's module
  beforeEach(function(){
    angular.module('ociWebPosApp',['ui.router']);
  });

  // instantiate service
  var Flash;
  beforeEach(inject(function(_Flash_) {
    Flash = _Flash_;
  }));

  it('should do something', function() {
    expect( !! Flash).toBe(true);
  });

});

我在测试中遇到的问题是:

PhantomJS 1.9.7 (Windows 7) Service: Flash should do something FAILED
        Error: [$injector:unpr] Unknown provider: FlashProvider <- Flash
        http://errors.angularjs.org/1.2.16/$injector/unpr?p0=FlashProvider%20%3C
-%20Flash

我已经在stackoverflow中看到了考虑案例的解决方案,但我不认为这是问题,因为它非常简单'Flash'

我做错了什么?

2 个答案:

答案 0 :(得分:1)

解决了!

我有2个混合问题。首先不注入依赖项,并在调用它们时做错了。

由于自耕农似乎自动添加了所有内容,我认为依赖关系也在这里添加。我错了。

describe('Service: Flash', function() {

  // load the service's module
  beforeEach(module('ociWebPosApp');
  });
...

然后在karma.conf.js中添加依赖项

...
files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/bower_components/angular-animate/angular-animate.js',
      'app/bower_components/angular-cookies/angular-cookies.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/bower_components/angular-route/angular-route.js',
      'app/bower_components/angular-sanitize/angular-sanitize.js',
      'app/bower_components/angular-touch/angular-touch.js',


      'app/bower_components/angular-ui-router/release/angular-ui-router.js',


      'app/scripts/**/*.js',
      'test/mock/**/*.js',
      'test/spec/**/*.js'
    ],
...

答案 1 :(得分:-1)

Flash未定义,因为它已在其他功能范围中定义

'use strict';

describe('Service: Flash', function() {
    var Flash = null

  // load the service's module
  beforeEach(function(){
    angular.module('ociWebPosApp',['ui.router']);
  });

  // instantiate service
  beforeEach(inject(function(_Flash_) {
    Flash = _Flash_;
  }));

  it('should do something', function() {
    expect( !! Flash).toBe(true);
  });

});