【问题标题】:How to correct this AngularJS Karma test setup filter injection如何更正此 AngularJS Karma 测试设置过滤器注入
【发布时间】:2013-12-16 22:04:00
【问题描述】:

我一直在学习 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:unpr] 未知提供者:hoursFilterProvider

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

我认为由于缺乏经验而遗漏了一些东西,任何帮助都会很棒!

【问题讨论】:

  • 我还不太擅长测试,但是 egghead.io 有很多关于这方面的教程可能会有所帮助。

标签: angularjs karma-runner angularjs-filter


【解决方案1】:

您应该在测试中使用 angular.mock.module。 看我的回答here

【讨论】:

  • 啊,我认为在 angular.js 之后包含 angular-mocks.js 会覆盖我。我会尽快尝试...谢谢!
  • 如果你在曼彻斯特,我欠你一个拥抱!效果很好。
猜你喜欢
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 2013-05-15
  • 2016-11-13
  • 1970-01-01
相关资源
最近更新 更多