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();
}
});
});
但是,当我运行它,它给了我一个错误,加载不知道一个对象。你有什么想法如何进行测试?
感谢