你需要间谍来测试 Jasmine 中是否调用了一个函数吗?
Do you need spies to test if a function has been called in Jasmine?
正在学习 Jasmine,想知道以下测试是否有效?如果没有,有人可以解释为什么吗?我已经阅读了很多教程,但找不到一个很好的解释来帮助我理解为什么我似乎无法正确编写如下所示的测试。
// 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 在哪里定义的?您需要以某种方式引用它。
// 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,想知道以下测试是否有效?如果没有,有人可以解释为什么吗?我已经阅读了很多教程,但找不到一个很好的解释来帮助我理解为什么我似乎无法正确编写如下所示的测试。
// 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 在哪里定义的?您需要以某种方式引用它。
// 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();