1
我有以下代碼:如何使用特定的window.location.href來測試Jasmine中的window.load()?
window.onload= function() {
var requestData = {
bookId:$('.bookId').text()
};
if(window.location.href.indexOf("/viewbook")!=-1){
$.post("check-book", requestData, function (response) {
if(response == "already saved") {
$('.add-btn').attr('disabled', 'disabled');
}
});
}
};
,我試圖寫在茉莉花一個合適的測試。所以,我有以下代碼:
describe("should disable button", function() {
it("should check if book exists in list while loading page", function() {
var localContext = {
"window":{
location:{
href:"/viewbook"
}
}
}
with(localContext){
spyOn($, 'post');
window.load();
expect(window.location.href).toBe("/viewbook");
//expect($.post).toHaveBeenCalled();
}
});
});
但是,當我運行它,它給了我一個錯誤,加載不知道一個對象。你有什麼想法如何進行測試?
感謝