我正在使用 js-fixture、Mocha、Chai、Sinon 和 Karma。
我能够在包装器中看到正确的元素。
但是,当我触发点击事件时,我的点击事件永远不会被触发。
我假设事件永远不会绑定到元素。
我对固定装置做错了吗?有没有更好的方法来用 mocha 测试 jQuery 事件?
测试:
var thisFixture,
wrapper;
describe("modal is removed when close button is clicked", function () {
beforeEach(function () {
fixtures.path = "/base/spec/javascripts/fixtures";
fixtures.load('modal.html');
thisFixture = $(fixtures.body());
wrapper = $(thisFixture[0]);
});
it("should is removed from the dom", function () {
debugger;
h2uModal.bindEvents();
var close = wrapper.find(".fa-times");
expect(wrapper.size()).to.equal(1);
close.trigger("click");
expect(wrapper.size()).to.equal(0);
});
afterEach(function () {
fixtures.cleanUp();
});
});
被测代码:
var h2uModal = {
bindEvents: function () {
$("#modal .fa-times").click(function () {
$(this).parents("#wrapper").remove();
});
}
};
夹具:
<div id='wrapper'>
<div id='modal'>
<i class='fa-times'></i>
</div>
</div>
结果:
Chrome 34.0.1847 (Mac OS X 10.9.2) modal is removed when close button is clicked should is removed from the dom FAILED
AssertionError: expected 1 to equal 0