【发布时间】:2016-06-11 23:46:18
【问题描述】:
我创建了一个自定义指令,并想模拟一个变量以使测试正常工作。这是单元测试的一部分:
it('should pass on initialvalue', function() {
scope.$apply(function() {
scope.initial = 2;
scope.Repairer = null;
});
expect(elementScope.initial).toEqual(2);
});
该指令在设置初始变量时调用服务。两个测试都失败了,因为在指令中我有一个名为 request 的变量没有被正确模拟。问题是我怎样才能模拟出来?还是我需要将请求变量放在范围内?这是代码的一部分:
if (scope.Repairer) {
console.log('calling scriptservice');
var request = {
allocationProcess: (scope.$parent.lodgement.searchSettings.automatic.visible) ? 'automatic' : 'direct',
allocationSource: 'internal',
brand: brandScriptTag, // lookup brand scripting name
includeSegment: false,
relationship: scope.repairer.relationshipCode,
ruralSearch: scope.repairer.isRural,
state: scope.$parent.lodgement.claimLocation
};
scriptingService.getScript(request).then(function (scripts) {
scope.scripts = scripts;
});
} else {
scope.scripts = null;
}
plunker ref:http://plnkr.co/edit/juDwpot727jzxzzWeaJl?p=preview
【问题讨论】:
标签: angularjs unit-testing angularjs-directive jasmine spy