Angular.js单元/集成测试-触发链接功能
Angular.js Unit/Integration Testing - Trigger Link Function
目前,我正在为一个指令编写一个测试(使用Jasmine),我怀疑链接函数没有被触发。
指令如下:
.directive('userWrapperUsername', [
'stringEntryGenerateTemplate',
'stringEntryGenerateLinkFn',
// UserWrapper username column
// Attribute: 'user-wrapper-username'
// Attribute argument: A UserWrapper object with a 'newData' key into an
// object, which contains a 'username' key holding the
// UserWrapper's username
function(stringEntryGenerateTemplate, stringEntryGenerateLinkFn) {
return {
template: stringEntryGenerateTemplate('username'),
restrict: 'A',
scope: true,
link: stringEntryGenerateLinkFn('userWrapperUsername', 'username')
};
}
])
因此,它使用了工厂提供的两个功能,即stringEntryGenerateTemplate
和stringEntryGenerateLinkFn
。
stringEntryGenerateTemplate
函数接收一个字符串并返回一个字符串。
stringEntryGenerateLinkFn
函数在被调用时返回实际的链接函数。它主要由事件处理程序组成,所以我将把它简化为:
function stringEntryGenerateLinkFn(directiveName, key) {
return function(scope, element, attr) {
scope.state = {};
scope.userWrapper = scope.$eval(attr[directiveName]);
}
}
以下是我如何使用指令:
<div user-wrapper-username="u"></div>
这是我的测试用例:
describe('UserWrapper Table string entry', function() {
var $scope
, $compile;
beforeEach(inject(function($rootScope, _$compile_) {
$scope = $rootScope.$new();
$compile = _$compile_;
}));
it('should be in stateDisplay if the value is non empty', function() {
var userWrapper = {
orgData: {
student: {
hasKey: true,
value: 'abcdef'
}
},
newData: {
student: {
hasKey: true,
value: 'abcdef',
changed: false
}
}
}
, key = 'student'
, elem
, elemScope;
$scope.userWrapper = userWrapper;
elem = $compile('<div user-wrapper-username="userWrapper"></div>')($scope);
elemScope = elem.scope();
expect(elemScope.userWrapper).toBe(userWrapper);
expect(elemScope.state).toEqual(jasmine.any(Object)); // this fails
});
});
所以我得到一个测试失败,说elemScope.state
是未定义的。回想一下,我有一个scope.state = {};
语句,如果执行了链接函数,就应该执行它。我在链接函数中尝试了console.log
,但它没有被执行。
那么我该如何触发链接功能呢?
谢谢!
事实证明,我必须初始化包含工厂stringEntryGenerateTemplate
和stringEntryGenerateLinkFn
的模块,该模块与包含userWrapperUsername
指令的模块相同,方法是将其添加到我的测试用例中:
beforeEach(module('userWrapper', function() {}));
其中CCD_ 11是模块的名称。
因此,测试用例变成:
describe('UserWrapper Table string entry', function() {
var $scope
, $compile;
beforeEach(module('userWrapper', function() {}));
beforeEach(inject(function($rootScope, _$compile_) {
$scope = $rootScope.$new();
$compile = _$compile_;
}));
it('should be in stateDisplay if the value is non empty', function() {
var userWrapper = {
orgData: {
student: {
hasKey: true,
value: 'abcdef'
}
},
newData: {
student: {
hasKey: true,
value: 'abcdef',
changed: false
}
}
}
, key = 'student'
, elem
, elemScope;
$scope.userWrapper = userWrapper;
elem = $compile('<div user-wrapper-username="userWrapper"></div>')($scope);
elemScope = elem.scope();
expect(elemScope.userWrapper).toBe(userWrapper);
expect(elemScope.state).toEqual(jasmine.any(Object)); // this fails
});
});
这对我来说似乎是一个很大的疏忽。希望这将帮助任何面临类似问题的人。
相关文章:
- 如何绑定多个具有相同敲除功能、传递不同ID的链接
- 功能点击使打开新链接,不转到原始链接
- 修复Javascript代码以将文本中的所有Youtube链接转换为Youtube ID功能
- 如何在用户提交链接时添加提取推文并将其发布到站点的功能
- 具有双重功能的按钮:在_blank窗口中打开一个链接,再加上_self中的另一个链接
- 通过模糊功能传递链接,而不是单击另一个链接
- AngularJS:在指令's链接功能
- DOM在指令中未就绪's链接功能.hacky Timeout是唯一的解决方案吗
- 链接功能中的角度单元测试$attrs
- jQuery:使用 var 快捷方式分配数据以及如何链接功能以在加载和单击时运行
- 角度链接功能:$Scope与范围
- angularjs链接功能的用途
- 谷歌硬盘的网络主机链接功能有被淘汰的危险吗
- 为什么我们需要Angular指令's链接功能
- Angular.js单元/集成测试-触发链接功能
- 可以混合使用模板和链接功能
- 锚链接功能-onclick和href
- 链接功能作为承诺
- 执行指令的参数's的链接功能依赖于DI
- 如何重画图表:d3.js链接功能和交叉过滤过滤