【问题标题】:Jasmine test for Angular directive not workingAngular 指令的 Jasmine 测试不起作用
【发布时间】:2015-10-21 22:11:38
【问题描述】:

我正在尝试对 Angular 指令进行非常基本的测试,但似乎无法让它运行。我的代码如下。我从 jshint 看到的错误是:'inject' is not defined,而我的测试失败的错误是:TypeError: object is not a function

'use strict';

describe('playerInfo directive', function() {
  var $compile;
  var $rootScope;

  beforeEach(module('gameApp'));

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

  it('should replace the element with the appropriate content', function() {
    var element = $compile('<dm-player-info></dm-player-info>')($rootScope);
    $rootScope.$digest();
    expect(element.html()).toContain('Member Since:');
  });
});

一旦到达module,测试就会失败并显示上述错误消息。很明显,测试并没有发现包含angular-mocks。但是,我在我的index.html 中都引用了它,并使用ngMock 作为应用程序依赖项进行了测试(这会使我的整个应用程序崩溃但仍然无法运行测试)和没有。

更新:我目前正在使用 jasmine-node 进行测试。可能是这个问题,需要加jasmine。

【问题讨论】:

  • 你是否包含了 angular-mocks.js?
  • 我做到了。虽然当我在我的应用程序模块中添加 ngMock 作为依赖项时,整个应用程序停止运行(空白页面,控制台中没有错误)。
  • 在规范文件中包含内容的顺序也很重要。在包含测试 js 文件之前是否包含 angular-mocks

标签: javascript angularjs node.js unit-testing jasmine


【解决方案1】:

我刚刚遇到了这个问题,这就是让我开心的原因(实际上是在最后)。只需添加module('ng')module('yourAppModule')。我把它们放在每个块之前。它应该可以工作。

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2018-09-30
    • 2017-05-25
    • 2020-11-20
    • 2013-08-31
    相关资源
    最近更新 更多