phantomjs-jasmineを使用して簡単なテストを行う
//example_spec.js
describe("Click button", function() {
it ("should be become 3", function() {
var i = 0;
var button_element = $('#button');
console.log(button_element.text());
while(i < 3 ) {
button_element[0].click();
console.log($('#counter').text());
i ++;
}
console.log($('#counter').text());
expect($('#counter').text()).toEqual('3');
});
});
//example.js
var main = function() {
var button = document.getElementById('button');
button.addEventListener('click', function(){
var count = document.getElementById('counter');
count.innerText = parseInt(count.innerText) + 1;
});
}
window.addEventListener('load', main);
window.addEventListener('load', main);
//index.html
....
<p id='counter'>0</p>
<button id='button'></button>
....
テスト結果は本当に奇妙です
hantomjs lib/run_jasmine_test.coffee spec/TestRunner.html
Starting...
0
0
0
Click button : should be become 3
Error: Expected '1' to equal '3'.
Finished
-----------------
1 spec, 1 failure in 0.033s.
ConsoleReporter finished
私のコードで何かが間違っているに違いありません、何か考えはありますか?
//example-updated-jquery-version.js
var main = function() {
var button = $('#button');
$('#button').on('click', function(){
$('#counter').text(parseInt($('#counter').text()) + 1);
})
}