您需要间谍来测试是否已在Jasmine中调用了函数吗? [英] Do you need spies to test if a function has been called in Jasmine?
本文介绍了您需要间谍来测试是否已在Jasmine中调用了函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习茉莉花,想知道以下测试是否有效?如果没有,有人可以解释为什么吗?我一直在阅读许多教程,找不到很好的解释,这可以帮助我理解为什么我似乎无法正确编写下面的测试.
Am learning Jasmine, wondering if the following test would be valid? And if not, can someone explain why? I've been reading a number of tutorials and can't find a good explanation that has helped me understand why I can't seem to write a test like the one below correctly.
// spec
describe("when cart is clicked", function() {
it("should call the populateNotes function", function() {
$("#show-cart").click()
expect(populateNotes()).toHaveBeenCalled();
})
})
// code
$("#show-cart").click(function() {
populateNotes();
})
推荐答案
您需要做两件事,首先需要在单击之前监视该函数.通常,您会监视像这样的函数,该函数是对象的成员. populateNotes在哪里定义?您需要以某种方式对其进行引用.
You need to do two things, first you need to spy on the function before the click. Normally you would spy on a function like this that is a member of an object. Where is populateNotes defined? You need a reference to it somehow.
// This might work, if the function is defined globally.
spyOn(window, 'populateNotes');
// Then do your action that should result in that func being called
$("#show-cart").click();
// Then your expectation. The expectation should be on the function
// itself, not on the result. So no parens.
expect(window.populateNotes).toHaveBeenCalled();
这篇关于您需要间谍来测试是否已在Jasmine中调用了函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文