AngularJS Karma Jasmine测试指令因$compile而失败?

AngularJS Karma Jasmine测试指令因$compile而失败?,angularjs,jasmine,karma-runner,Angularjs,Jasmine,Karma Runner,我正在用Jasmine为angularJS中的指令编写一个测试。在karma.conf.js中,我包含了所有依赖项和模块的路径。我使用的指令非常复杂,因此我借用了一些简单的自定义指令代码进行测试。它在我的应用程序中的js fiddle和指令函数中工作得很好,但在我的应用程序中没有通过以下测试: 我的规格如下所示: describe('Unit testing: ', function() { var $compile, $rootScope; beforeEach(modul

我正在用Jasmine为angularJS中的指令编写一个测试。在karma.conf.js中,我包含了所有依赖项和模块的路径。我使用的指令非常复杂,因此我借用了一些简单的自定义指令代码进行测试。它在我的应用程序中的js fiddle和指令函数中工作得很好,但在我的应用程序中没有通过以下测试:

我的规格如下所示:

describe('Unit testing: ', function() {
  var $compile,
      $rootScope;

 beforeEach(module('app.directives'));  // our directives

  // Store references to $rootScope and $compile
  // so they are available to all tests in this describe block
  beforeEach(inject(function(_$compile_, _$rootScope_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $compile = _$compile_;
    $rootScope = _$rootScope_;
  }));

  it('Replaces the element with the appropriate content', function() {
    // Compile a piece of HTML containing the directive
    var element = $compile("<div hello-world></div>")($rootScope);

    // fire all the watches, so the scope expression {{1 + 1}} will be evaluated
    $rootScope.$digest();

    // Check that the compiled element contains the templated content
    expect(element.html()).toContain("Hello World!");
  });
});
angular.module('app.directives')
.directive('helloWorld', function() {
  return {
      restrict: 'AE',
      template: '<h3>Hello World!!</h3>'
  };
});
当我插入一些console.log语句时,我看到的主要问题是,在调用$compile之后(或者在调用$digest之后),指令没有正确编译:

Firefox40.0.0(MacOSX10.10.0)日志:{0:,长度:1}
我被难住了。我知道我可能需要提供更多关于我的应用程序的信息,但简而言之,这个解释是我的核心问题

[编辑]:创建一个新模块是解决这个问题的一个快速方法,但是当一个模块中有多个指令(仍然失败)时,如何处理指令单元测试仍然是一个问题。

(答案已编辑) 一旦我确定karma.conf.js中正确地包含了你的代码,你的代码就为我工作了

files: [
    "include/path/to/your/directive/file.js",
    "include/path/to/your/directive/test.js"
]

您的指令名为“mmsHelloWorld”,所以您的模板不应该看起来像
?也就是说,您缺少了
mms-
prefixmms只是我正在处理的应用程序特有的命名约定。我试图概括代码,使其更直观,所以我删除了它,但显然不是全部!很抱歉您的指令名应该是“helloWorld”,而不是“helloWorld”,虽然我不完全确定前导大写字母是否有区别,但比抱歉更安全:)您是对的,但这也是命名约定编辑的一部分。在发布代码之前,我应该更加注意:(.Ah,这是
replace:'false'
'false'
字符串的计算结果为
true
。要么设置
replace:false
,要么完全忽略
replace
(无论如何,它是不推荐的)很好的建议,但是内部html仍然是空的。我也尝试了angular.element方法。因此,这个问题与建议的karma配置文件有关。文件的顺序、依赖项和构建目录都必须进行重构才能正常工作。
Firefox 40.0.0 (Mac OS X 10.10.0) LOG: {0: <div class="ng-scope" hello-world=""></div>, length: 1}
files: [
    "include/path/to/your/directive/file.js",
    "include/path/to/your/directive/test.js"
]