单元测试的正确语法是什么?
当我在浏览器中打开它时,下面的代码会起作用。其目的是每当输入框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);
}
}这是事件监听器。当然,它返回一个函数。
注意,函数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",因此测试失败。
如果我理解正确的话,问题是测试调用的是事件侦听器的返回值,而不是函数,但我不知道如何从测试中正确地调用函数。
提前谢谢。
发布于 2013-12-10 10:45:07
ActionInputBox应该是匿名函数中的包装:
ib.addEventListener('change', function(){Action01InputBox (ib, c5)}, false);https://stackoverflow.com/questions/14478837
复制相似问题