0
单元测试的正确语法是什么?Javascript单元测试addEventListener语法
下面的工程,当我在浏览器中打开它。这样做的目的是为了阅读时,它改变输入框ib
的内容,以及它的一个解释值写入表格单元格c5
...
var c5 = document.createElement('td');
var ib = document.createElement('input');
// For all browsers except IE before Version 9 -see http://help.dottoro.com/ljeuqqoq.php
if (ib.addEventListener)
{
ib.addEventListener('change', Action01InputBox (ib, c5), false);
}
// For IE before Version 9 -see http://help.dottoro.com/ljeuqqoq.php
else {
if (ib.attachEvent){
ib.addEventListener('change', Action01InputBox (ib, c5), false);
}
}
这里的事件监听器。当然,它会返回一个函数。
NB功能EmptyNode(c5)
简单地删除目标节点的所有内容,并通过RealNumberFromInput (ib.value)
一个正则表达式会从输入字符串实数...
function Action01InputBox (ib, c5)
{
return function()
{
EmptyNode(c5);
var r = RealNumberFromInput (ib.value);
c5.appendChild(document.createTextNode(r));
};
};
这里的单元测试...
Action01InputBoxTest = TestCase("Action01InputBoxTest");
Action01InputBoxTest.prototype.test01 = function()
{
// Text box
var r = 0.123;
var ib = document.createElement('input');
ib.setAttribute('value', document.createTextNode(r));
// Target cell
var c5 = document.createElement('td');
c5.appendChild(document.createTextNode("Bogus"));
// Do action
Action01InputBox(ib, c5);
assertEquals(c5.textContent, r);
};
c5
的textContent
应该从“Bogus”变为“0.123”,因此测试失败。
如果我理解正确,问题是测试调用事件监听器的返回值,而不是函数,但我无法弄清楚如何从测试中正确调用函数。
在此先感谢。