jam*_*mes 3 javascript jquery jasmine jasmine-jquery
正在学习 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();
})
Run Code Online (Sandbox Code Playgroud)
小智 5
您需要做两件事,首先您需要在点击之前监视该功能。通常,您会监视像这样作为对象成员的函数。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();
Run Code Online (Sandbox Code Playgroud)