如何使用Jasmine在javascript的函数中创建间谍变量



我为此输入字段创建了一个函数。我真的很纠结于如何使用茉莉花为这个函数编写测试用例。函数中的参数会根据它所在的输入字段而变化。(以下代码供参考(

<input type="text" class="ipClass" onfocusout="return 
outputFunction('uniqueIdOne') id="uniqueIdOne">
<input type="text" class="ipClass" onfocusout="return 
outputFunction('uniqueIdTwo') id="uniqueIdTwo">

function outputFunction(id){
var inputValue = document.getElementby(id).value;
if(value == "10")
return inputValue;
else
return somethingElse;
}

该函数是对象的属性吗?正如我所看到的,你可以为对象的功能创建间谍。

例如:

function Human(firstName, lastName) {
this.firstName = firstName,
this.lastName = lastName,
this.fullName = function() {
return this.firstName + " " + this.lastName;
}
}

在这里,您可以通过以下情况进行监视

var human = new Human('some', 'name');
spyOn(human, "fullName");

然后

expect(human.getName).toHaveBeenCalled();

请参阅"在茉莉花中,模拟被称为间谍。在Jasmine中创建间谍有两种方法:spyOn((只能在对象上已经存在该方法时使用,而jasmine.createSpy((将返回一个全新的函数:">

但如果你只是想知道如何在茉莉花中写间谍。与上面的链接相同

最新更新