我正在嘗試使用Sinon和CasperJS來測試我的超時功能。此頁面顯示在數字標牌上,因此它不是典型的網頁 - 它具有很長的使用壽命,因此超時值很高。sinon.useFakeTimers不會觸發超時
下面是我試圖測試相關代碼:
RiseVision.Image = (function() {
// Private
function startTimer() {
setTimeout(function() {
var img = document.getElementById("image");
img.style.backgroundImage = "url(http://s3.amazonaws.com/images/logo-small.png?" + new Date().getTime() + ")";
}, 900000);
}
// Public
function ready() {
...
}
return {
"ready": ready
};
})();
我使用CasperJS對我的測試中,像這樣:
var e2ePort = system.env.E2E_PORT || 8099;
var url = "http://localhost:"+e2ePort+"/src/widget-e2e.html";
var clock;
casper.test.begin("Image Widget - e2e Testing", {
test: function(test) {
casper.start();
casper.thenOpen(url, function() {
test.assertTitle("Image Widget", "Test page has loaded");
});
casper.then(function() {
casper.waitFor(function waitForUI() {
return this.evaluate(function loadImage() {
// Wait for the background image to be set.
return document.getElementById("image").getAttribute("style") !== "";
});
},
function then() {
// Do some assertions here.
casper.waitFor(function waitForTimer() {
return this.evaluate(function expireTimer() {
clock = sinon.useFakeTimers();
clock.tick(900000);
return document.getElementById("image").getAttribute("style") !==
"background-image: url(http://s3.amazonaws.com/images/logo-small.png);";
});
},
function then() {
// More assertions here.
});
});
});
casper.run(function runTest() {
test.done();
});
}
});
我知道這個功能被執行,因爲我可以成功地從它裏面記錄消息,但它不會觸發我的計時器。如果我公開startTimer
函數,似乎沒有什麼區別。
任何想法?
Thx。
EDITED - 已更新以包含更多代碼。
謝謝。更新我的問題以包含更多的澄清代碼。乾杯。 – donnapep 2015-03-03 14:11:52