【发布时间】:2014-05-26 14:00:43
【问题描述】:
我在使用 templateUrl 的单元测试指令时遇到问题。
有这个很棒的关于 AngularJS 测试的教程 [1],它甚至还有一个与之配套的 Github 存储库 [2]
所以我将它分叉 [3] 并进行了以下更改:
在directives.js 我创建了两个新指令:
.directive('helloWorld', function() {
return {
restrict: 'E',
replace: true,
scope:{},
template: '<div>hello world!</div>',
controller: ['$scope', function ($scope) {}]
}
})
.directive('helloWorld2', function() {
return {
restrict: 'E',
replace: true,
scope:{},
templateUrl: 'mytemplate.html',
controller: ['$scope', function ($scope) {}]
}
})
我更改了test/unit/directives/directivesSpecs.js,以便将模板加载到 $templateCache 中,然后为新指令添加了另外两个测试:
//
// test/unit/directives/directivesSpec.js
//
describe("Unit: Testing Directives", function() {
var $compile, $rootScope, $templateCache;
beforeEach(angular.mock.module('App'));
beforeEach(inject(
['$compile','$rootScope', '$templateCache', function($c, $r, $tc) {
$compile = $c;
$rootScope = $r;
//Added $templateCache and mytemplate
$templateCache = $tc;
$templateCache.put('mytemplate.html', '<div>hello world 2!</div>');
}]
));
//This was already here
it("should display the welcome text properly", function() {
var element = $compile('<div data-app-welcome>User</div>')($rootScope);
expect(element.html()).to.match(/Welcome/i);
});
//Added this test - it passes
it("should render inline templates", function() {
var element = $compile('<hello-world></hello-world>')($rootScope);
expect(element.text()).equal("hello world!");
});
//Added this test - it fails
it("should render cached templates", function() {
var element = $compile('<hello-world2></hello-world2>')($rootScope);
expect(element.text()).equal("hello world 2!");
});
});
最后一次测试失败,因为 Angular 不会像它应该的那样编译模板。
$ grunt test:unit
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 35.0.1916 (Linux)]: Connected on socket ChISVr0ZABZ1fusdyv3m
Chrome 35.0.1916 (Linux) Unit: Testing Directives should render cached templates FAILED
expected '' to equal 'hello world 2!'
AssertionError: expected '' to equal 'hello world 2!'
Chrome 35.0.1916 (Linux): Executed 18 of 18 (1 FAILED) (0.441 secs / 0.116 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
我很确定这应该有效。 至少,它与@SleepyMurth 在[4] 上提出的解决方案非常相似。
但我觉得我已经达到了理解我目前对 AngularJS 的了解出了什么问题的极限。
帮助! :-)
[1]http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html
[2]https://github.com/yearofmoo-articles/AngularJS-Testing-Article/
【问题讨论】:
-
那么,
element.text()返回什么? -
抱歉,我会更新问题以包含该问题(它是一个空字符串)
标签: javascript angularjs unit-testing gruntjs karma-runner