![](/img/trans.png)
[英]karma/jasmine/angular – Trouble unit-testing directives AngularJS that use templateUrl
[英]Trouble unit-testing directives that use templateUrl
我在使用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/
[3] https://github.com/tonylampada/AngularJS-Testing-Article
當templateUrl
指定,模板使用被取出$http
(即使是緩存的模板, $http
從供應$templateCache
)。 出於這個原因, $http
的承諾需要有一個$ digest循環,用模板內容解析並由指令處理。
由於promises在$ digest循環期間得到解決(並且因為我們在“Angular context”之外),我們需要在評估斷言之前手動調用$rootScope.$digest()
。
因此,最后一次測試應該像這樣修改:
it("should render cached templates", function() {
var element = $compile('<hello-world2></hello-world2>')($rootScope);
$rootScope.$digest(); // <-- manually force a $digest cycle
expect(element.text()).toBe(tmplText);
});
另見這個簡短的演示 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.