Ton*_*ada 3 javascript unit-testing angularjs gruntjs karma-runner
我在使用单元测试指令时遇到了麻烦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) {}]
}
})
Run Code Online (Sandbox Code Playgroud)
我改变了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!");
});
});
Run Code Online (Sandbox Code Playgroud)
最后一次测试失败,因为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.
Run Code Online (Sandbox Code Playgroud)
我很确定这应该有效.至少,它与@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
gka*_*pak 10
当templateUrl指定时,模板是使用取出的$http(即使对于高速缓存的模板,其中$http供应来自$templateCache).出于这个原因,需要有一个$ digest循环来$http承诺使用模板内容解析并由指令处理.
由于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);
});
Run Code Online (Sandbox Code Playgroud)
另见这个简短的演示.
| 归档时间: |
|
| 查看次数: |
544 次 |
| 最近记录: |