gpt4 book ai didi

javascript - Angular.js 单元/集成测试 - 触发链接功能

转载 作者:行者123 更新时间:2023-11-28 20:56:08 25 4
gpt4 key购买 nike

目前,我正在为指令编写测试(使用 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')
};
}
])

因此它利用了工厂提供的两个函数,即stringEntryGenerateTemplatestringEntryGenerateLinkFn

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,但它也没有执行。

那么如何触发链接功能呢?

谢谢!

最佳答案

事实证明,我必须初始化包含工厂 stringEntryGenerateTemplatestringEntryGenerateLinkFn 的模块,这与包含 userWrapperUsername 的模块相同通过将其添加到我的测试用例中来指令:

beforeEach(module('userWrapper', function() {}));

其中 userWrapper 是模块的名称。

所以测试用例变成:

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
});
});

这似乎是我的一个很大的疏忽。希望这会帮助面临类似问题的任何人。

关于javascript - Angular.js 单元/集成测试 - 触发链接功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17737721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com