Bit*_* Ho 12 javascript unit-testing jasmine angularjs
如何在angularjs中对指令的$ destroy事件进行单元测试?
我的指令中有代码:
scope.$on('$destroy', function () {
//clean something
});
Run Code Online (Sandbox Code Playgroud)
我的测试代码:
it('on destroy',function(){
scope.$destroy();
scope.$digest();
//expect everything done?
});
Run Code Online (Sandbox Code Playgroud)
任何建议!
Mic*_*ord 11
您应该测试在$destroy事件中执行的代码.这是一个使用控制器的人为例子:
测试
it('sets destroyed to true when the scope is destroyed', function() {
// Arrange
$scope.destroyed = false;
// Act
$scope.$destroy();
// Assert
expect($scope.destroyed).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
调节器
app.controller('MainCtrl', function($scope) {
$scope.$on('$destroy', function() {
$scope.destroyed = true;
});
});
Run Code Online (Sandbox Code Playgroud)
这里的人物.
小智 10
您可以从指令的模板中选择DOM并获取其范围,然后运行$ destroy().
例如:
你的tpl:
"<div id='tuna'></div>"
Run Code Online (Sandbox Code Playgroud)
你的考试:
it('test on destroy', function(){
var isolateScope = $(element).find('#tuna').eq(0).scope();
isolateScope.$destroy();
})
Run Code Online (Sandbox Code Playgroud)
希望能帮到你!
| 归档时间: |
|
| 查看次数: |
8345 次 |
| 最近记录: |