我正在尝试实现一个简单的 Jasmine 测试,其中 Jasmine 将测试某些代码是否在单击输入按钮时运行。但我不明白为什么点击没有触发?我意识到如果我在 beforeEach 中只有 .click()
函数就可以了,但我不认为它应该是这样工作的。
规范
describe("export citations", function (){
var btn
beforeEach(function(){
btn= $("input#myButton").eq(0);
});
it("should call click function", function() {
btn.trigger("click");
expect($("#content").length).toBeGreaterThan(0);
});
});
夹具
$(function(){
$("input#myButton").click(function(e){
//Run a bunch of code here
}
});
最佳答案
您是否真的在 fixture 中向 DOM 添加了一个元素?你也错过了一个);在夹具中关闭点击回调。
这对我有用:
describe("export citations", function () {
var btn;
beforeEach(function() {
btn= $("input#myButton").eq(0);
});
it("should call click function", function() {
btn.click();
expect($("#content").length).toBeGreaterThan(0);
});
});
夹具
(function() {
$("body").html("<input id='myButton' type='button'>");
$("body").html("<div id='content'></div>");
$("input#myButton").click(function() {
$("#content").html("<p>Hello</p>");
});
})();
关于javascript - Jasmine 无法触发点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9425158/