单元测试javascript函数'在不知情的情况下被用于其他地方
Unit testing javascript function that's unknowingly used elsewhere
我刚开始单元测试,请耐心等待。
举以下"愚蠢"的例子。我在页面上创建了一个要将函数绑定到的元素对象。我为test.myFunction设置了一个单元测试,并确保它返回"textToReturn"。
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);
}
});
一年后,我再次访问,并决定#element2每次都应该返回相同的文本,所以我在函数中对其进行硬编码,并更新原始对象的那部分:
{
object: '#element2',
event: 'keyup',
selector: '',
data: '',
theFunction: function() {
test.myFunction()
}
}
我忘记了这个函数也用于#element1,并相应地更新了我的单元测试,所以它总是返回相同的文本。
单元测试会通过,但#element1不会按预期工作。
我是不是错过了什么?或者这会在功能测试中被发现,因为它超出了单元测试的范围?如有任何建议,我们将不胜感激。
我不确定我是否完全理解您的代码,但一般来说,"单元测试"应该测试尽可能低级别的函数。在这种情况下,我认为这是test.myFunction()
。所以,如果在过去它接受了一个论点,然后基于这个论点返回了一些东西,而现在它没有,那么你是否传递任何东西都无关紧要。换句话说,如果JavaScript函数根本不使用参数,那么传入(或不传入)它们就无关紧要了(因此不会影响测试的成功)
在您的情况(如上)中,在代码更改为不再使用输入之后使用test.myFunction(textToReturn)
并不重要,因为无论哪种方式,该方法都可以正常工作。也就是说,假设您在theFunction()
中有一些代码,期望正确的textToReturn
值,那么您将为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") );
}
对于上面的源代码,我将对每个函数进行测试,包括通过测试和预期失败的测试。以下是一些测试(还有更多):
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");
});
请注意,第四次测试可能会失败,从而突出了我忘记更新baz()
的事实。
- 如何在未直接触发的情况下停止事件
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- 在不阻止默认行为的情况下检测IE10中的缩放
- 如何在不传递此信息的情况下查找被调用的元素
- 如何在不刷新页面的情况下更新显示框
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 在我的情况下,如何进行http请求
- 在不知道深度或父属性的情况下从对象中删除属性
- 如何在没有数据标记的情况下将bootstrap 3 collapse用于手风琴
- 用于 HTML 表单验证的正则表达式在没有空格的情况下开始
- GULP:如何在不刷新的情况下更新浏览器(仅适用于 CSS 更改)
- 如何在不丢失Jssor图像滑块的情况下更改其尺寸'其响应特性、图像纵横比等仅适用于iPhone设备
- 表单验证不起作用,用于在未经验证的情况下提交
- 单元测试javascript函数'在不知情的情况下被用于其他地方
- .htaccess用于SEO机器人在没有哈希标签的情况下抓取单页应用程序
- 在没有jQuery用于选择所有下拉列表的情况下,相当于$(“select”)
- 在不添加新标签/元素的情况下向属性添加描述(用于屏幕阅读器)