1
我在學習如何用Jasmine進行單元測試。我創建了一個非常簡單的示例,我嘗試測試在觸發jQuery點擊事件時調用方法。我似乎無法得到這個工作。有人可以幫忙嗎?非常感謝!!茉莉花單元測試不能用簡單的jquery點擊
我收到以下錯誤:
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();
});
});