我正在尝试学习如何使用 Jasmine 进行单元测试。我设计了一个非常简单的示例,在该示例中,我尝试测试触发 jquery click 事件时是否调用了方法。我似乎无法让它工作。有人可以帮忙吗?非常感谢!!
我收到以下错误:
ReferenceError: Butthead is not defined in http://localhost:4243/spec/buttheadSpec.js (line 11)
和
spyOn could not find an object to spy upon for responseAlert()
这是代码
function Butthead(){
}
Butthead.prototype.responseAlert = function(){
alert('You are a butthead');
}
$(document).ready(function(){
var butthead = new Butthead();
$('#myButton').click(function(){
butthead.responseAlert();
});
}
这是我的单元测试
// Test Fixture
describe('When clicking myButton', function(){
var butthead;
// Set up
beforeEach(function(){
butthead = new Butthead();
});
// Test
it('should say Hello', function(){
spyOn(butthead, 'responseAlert');
$('#myButton').click();
expect(butthead.responseAlert).toHaveBeenCalled();
});
});