Javascript 单击调用函数的事件
我正在创建一个Jasmine测试,以查看在单击对象Javascript 单击调用函数的事件,javascript,jquery,jasmine,Javascript,Jquery,Jasmine,我正在创建一个Jasmine测试,以查看在单击对象$(“#通知”)时是否正在调用该函数 以下是.cshtml文件中的代码: $('#notification').click(function() { showNotification(); }); 以下是我目前的茉莉花测试: describe("Notification", function () { beforeEach(function () { loadFixtures('dashboard.cshtml
$(“#通知”)
时是否正在调用该函数
以下是.cshtml
文件中的代码:
$('#notification').click(function() {
showNotification();
});
以下是我目前的茉莉花测试:
describe("Notification", function () {
beforeEach(function () {
loadFixtures('dashboard.cshtml');
});
it("should call function when notification is clicked", function() {
spyOn(window, 'showNotification');
$('#notification').click();
expect(window.showNotification).toHaveBeenCalled();
});
});
然而,我得到了这个结果:
Test 'Notification:should call function when notification is clicked' failed
Expected spy showNotification to have been called.
我做错了吗?我错过什么了吗?我是茉莉花的新手
编辑:以下是showNotification的外观
function showNotification () {
loadNotification('', '');
resetFields();
}
编辑:找到我的问题的解决方案。我使用以下代码来模拟单击
$('#notification').on('click', function () {
showNotification();
});
$('#notification').click();
我现在可以测试是否调用showNotification,以及showNotification中的loadNotification和Reset字段
感谢那些回应的人。有人会关闭此按钮吗?结果显示,您将侦听器添加到按钮中,但从未使用单击操作触发它 添加此单击操作以触发测试:
$('#通知')。触发('click')代码>确保window.showNotification
存在,并且该函数未嵌套在另一个函数中。确保该函数未嵌套,这是此处的重点-可以显示fixtures文件吗?仪表板cshtml@DarrenSweeney我不确定是否允许我在dashboard.cshtml中显示代码,但showNotification类似于函数showNotification(){loadNotification(“”,);resetFields();}Ok,不确定“允许”是什么意思,但如果需要帮助,则需要能够提供与问题相关的所有代码。Dashboard.cshtml实际上不是一个fixture文件。这是一个正在我们的项目中使用的文件。我是不是走错路了?我应该创建一个实际的夹具文件吗?我的意思是,如果我共享整个cshtml文件,我可能会违反公司的规定。它是一个包含大量Java脚本和对象的大文件。我是否将它放在$(“#notification”)之后。单击()?请阅读作品。你认为$(“#通知”)怎么样。点击()代码>是什么?