我有一个用 javascript 编写的方法,我正在使用 Jasmine 来测试该方法。
该方法是一个 void 类型,它正在调用另一个方法。
我必须测试该方法是否正在调用其他方法,当前方法返回 void。
我应该在期望子句中写什么来比较它。
sendMessage=function(data){
if(data!=null)
{
postMessage(data);
}
}
Jasmine 代码:
describe('unit test void method', function(){
it("sendMessage method should invoke the postMessage", function () {
expect(sendMessage("hello");
})
})
我应该把它和什么比较?
最佳答案
詹姆斯是对的。这是一个 spy 功能,尽管我使用了另一种方法。
在你的设置 beforeEach 函数的某个地方:
spyOn(YourObject, 'postMessage').and.callThrough();
YourObject
是包含该功能的任何对象。期望:
it('expects postMessage() to have been called', function () {
// make the call to this function
YourObject.postMessage();
// Check internal function
expect(YourObject.postMessage).toHaveBeenCalled();
});
关于javascript - void方法测试javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402812/