单元测试javascript函数';它在不知不觉中被用于其他地方
我刚刚开始单元测试,所以请容忍我 以下面的“愚蠢”为例。我在页面上创建了一个元素对象,我想将函数绑定到该对象。我为test.myFunction设置了一个单元测试,并确保它返回“textToReturn”单元测试javascript函数';它在不知不觉中被用于其他地方,javascript,unit-testing,functional-testing,Javascript,Unit Testing,Functional Testing,我刚刚开始单元测试,所以请容忍我 以下面的“愚蠢”为例。我在页面上创建了一个元素对象,我想将函数绑定到该对象。我为test.myFunction设置了一个单元测试,并确保它返回“textToReturn” var global = { o_bindings : [ { object: '#element1', event: 'keyup', selector: '', data: '', theFunction: func
var global = {
o_bindings : [
{
object: '#element1',
event: 'keyup',
selector: '',
data: '',
theFunction: function() {
test.myFunction(textToReturn);
}
},
// .... lots of other code ....
{
object: '#element2',
event: 'keyup',
selector: '',
data: '',
theFunction: function() {
test.myFunction(textToReturn);
}
}
]
};
$(document).ready(function() {
for (var i=0; i < global.o_bindings.length; i++) {
var o = global.o_bindings[i];
$(o.object).on(o.event, o.selector, o.data, o.theFunction);
}
});
我忘记了该函数也用于#element1,并相应地更新了我的单元测试,以便它总是返回相同的文本
单元测试将通过,但element1无法按预期工作
我错过什么了吗?或者这会在功能测试中出现,因为它超出了单元测试的范围?任何建议都将不胜感激。我不确定自己是否完全理解您的代码,但一般来说,“单元测试”应尽可能测试最低级别的函数。在本例中,我认为这是
test.myFunction()
。所以,如果在过去它接受了一个论点,然后根据这个论点返回了一些东西,而现在它没有,那么如果你传递了任何东西,这其实并不重要。换句话说,如果JavaScript函数根本不使用参数,那么传入(或不传入)参数就无关紧要(因此不会影响测试的成功)
在您的例子中(如上所述),在代码更改为不再使用输入后使用test.myFunction(texttoreurn)
实际上并不重要,因为该方法在任何一种情况下都可以正常工作。也就是说,假设您在theFunction()中有一些代码
如果希望返回正确的文本
值,那么您需要为函数()编写一个测试来检查这一点。下面是一个简单的示例:
// sort of like your `test.myFunction()`
function foo() {
return "static value";
}
// the place where you remembered to change it...
function bar() {
$("#" + id).text( foo() );
}
// the place where you forgot to change it
function baz(id) {
$("#" + id).text( foo("some other value") );
}
对于上面的源代码,我将对每个函数进行测试,包括通过的测试和预期失败的测试
请注意,第四个测试可能会失败,从而突出了我忘记更新baz()
的事实。如果发生这种情况,那么您忘记了为global.o_绑定
或使用它的东西编写单元测试。
// sort of like your `test.myFunction()`
function foo() {
return "static value";
}
// the place where you remembered to change it...
function bar() {
$("#" + id).text( foo() );
}
// the place where you forgot to change it
function baz(id) {
$("#" + id).text( foo("some other value") );
}
QUnit.test("make sure foo works", function(assert) {
var v = foo();
assert.equal(v, "static value", "The value returned by foo should be correct");
});
// I would have added this one after the change to your `test.myFunction()`
QUnit.test("make sure foo works with input", function(assert) {
var v = foo("some other value");
assert.equal(v, "static value", "The value returned by foo does not change with input");
});
QUnit.test("make sure bar works", function(assert) {
bar("theElementId");
var t = $("#theElementId").text();
assert.equal(t, "static value", "The value in the element should be correct");
});
/*
// this is the ORIGINAL test for baz()
// It would have failed after the code change
QUnit.test("make sure baz works", function(assert) {
baz("theElementId");
var t = $("#theElementId").text();
assert.equal(t, "some other value", "The value in the element should be correct based on input to foo()");
});
*/
// but I would CHANGE the test above to this:
QUnit.test("make sure baz works", function(assert) {
baz("theElementId");
var t = $("#theElementId").text();
assert.equal(t, "static value", "The value in the element should be correct");
});