如何纠正这种AngularJS Karma测试设置过滤器注入



我一直在学习AngularJS,现在是开始编写测试的时候了,但我遇到了第一个障碍。

我的因果报应配置文件是:

module.exports = function(config) {
  config.set({
    basePath: '..',
    // frameworks to use
    frameworks: ['mocha'],
    // list of files / patterns to load in the browser
    files: [
      'node_modules/chai/chai.js',
      'public/js/libs/jquery-1.9.1.js',
      'public/js/libs/angular.js',
      'test/libs/angular-mocks.js',
      'public/js/**/*.js',
      'test/angular/**/*.js'
    ],
    exclude: [],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

我有一个过滤器声明:

angular.module('timeMachine.filters',[])
  .filter('hours', function() {
  return function(input) {
    return input == 1
      ? '1 hour'
      : input + ' hours';
  }
});

还有一个测试:

var should = chai.should();
describe("The hours filter", function() {
    beforeEach(function() {
      angular.module('timeMachine.filters');
    });
    it('should be able to test', function(){
      true.should.equal(true);
    });
    it('should be able to inject an hours filter', inject(function($filter) {
      var hours = $filter('hours');
      expect(hours).not.to.equal(null);
    }));
});

然而,第二次测试失败,并显示消息:

[$injector:unp]未知提供程序:hoursFilterProvider<-小时过滤

在运行Angular应用程序的上下文中,这个过滤器是有效的。

我想我是因为缺乏经验而错过了什么,任何帮助都会很棒!

您应该在测试中使用angular.mock.module。请在此处查看我的答案

最新更新