【发布时间】: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