【问题标题】:Unit Testing Directive单元测试指令
【发布时间】:2014-01-15 21:06:41
【问题描述】:

我的一个单元测试有一些问题。我试图测试一个指令,并且试图在单元测试中获取对 $compile 提供程序的引用。我正在使用 grunt 和一个名为 grunt-contrib-testem 的特定包来执行所有单元测试。

这是此类插件(coffescript)的配置的sn-p

testem:
  unit:
    src: [
      "angular.js"
      "angular-mocks.js"
      "app.js"
      "directive.js"
      "spec.js"
    ]
    options:
      launch_in_dev: ["PhantomJS"]
      debug: true

和一个测试用例的sn-p:

beforeEach(module("myapp"));

beforeEach(inject(function($injector){
    rootScope = $injector.get("$rootScope");
    compile = $injector.get("$compile"); //<-here is the problem
}));

我看到的错误信息如下:

[$injector:unpr] Unknown provider: $windowsProvider <- $windows <- $http <- $compile

非常感谢。

【问题讨论】:

  • 您可能不需要手动调用注射器。可以只写 beforeEach(inject(function($rootScope, $compile){ - 但这不是错误的原因。您是否仔细检查过,angular-mocks.js 可用?
  • 我缺少的是这样一行:beforeEach(module('myApp'));
  • 对不起 beforeEach(module("myApp"));包括让我编辑帖子
  • 我知道不需要调用 $injector。我这样做是为了找出发生了什么。
  • 下一个猜测:你有没有使用任何模板文件?如果是这样:您必须通过 beforeEach(module('template.html') 包含它们

标签: unit-testing angularjs gruntjs


【解决方案1】:

尝试以这种方式注入它们

beforeEach(inject(function(_$compile_, _$rootScope_) {
    $compile = _$compile_;
    $rootScope = _$rootScope_;
}

【讨论】:

  • 同上:[$injector:unpr] 未知提供者:$windowsProvider
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 2020-02-14
  • 1970-01-01
  • 2014-08-06
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多