我正在使用jasmine框架测试window.onload函数。我的javascript代码如下 -
window.onload = windowonloadFunction();
function windowonloadFunction(){
if('WebSocket' in window){
document.getElementById('supportmsg').innerHTML = 'Hi';
var img = document.createElement("img");
img.src = "images/check.png";
document.getElementById('supportmsg').appendChild(img);
}
}
我的规范代码如下 -
describe('Test Suite for windowonloadFunction function',function(){
it("This test check all tasks performed on window.load",function(){
affix('label[id="supportmsg"]');
windowonloadFunction();
});
});
当我运行SpecRunner.html文件时,我收到错误 - TypeError:无法设置null
的属性'innerHTML'我的代码出了什么问题?
答案 0 :(得分:0)
这是我对windowOnLoadFunction()的实现。请关注jsfiddle here
var fun = {}
fun.windowonloadFunction = function() {
document.getElementById('supportmsg').innerHTML = 'Hi';
var img = document.createElement("img");
img.src = "http://www.hyundai.co.uk/test-import/model-profiles/small_cars/i10_premium_sweet_orange.png";
document.getElementById('supportmsg').appendChild(img);
};
describe("A suite", function() {
var element;
beforeEach(function() {
element = $("<div id= 'supportmsg'/>");
$(document.body).append(element);
});
it("contains spec with an expectation", function() {
spyOn(fun, 'windowonloadFunction').and.callThrough();
fun.windowonloadFunction();
expect(fun.windowonloadFunction.calls.count()).toEqual(1);
});
});
&#13;
观察: