在QUnit测试中设置$(this)的范围
||
您如何在QUnit测试环境中设置正确的作用域以测试回调函数?
要测试的代码:
<script type=\"text/javascript\">
APP = {};
APP.callBack = function() {
$(this).closest(\"input\").val(\'foobar\');
};
$(function() {
$(\"#button\").click(APP.callBack);
});
</script>
<div>
<a id=\"button\" href=\"#\"></a>
<input id=\"id-for-testing-only\" name=\"test\" type=\"text\" value=\"barfoo\" />
</div>
测试代码:
test(\"try callback with \'this\' scope\", function() {
APP.callBack();
equals($(\"#id-for-testing-only\").val(), \"foobar\", \"should set value to \'foobar\'\");
});
没有找到相关结果
已邀请:
2 个回复
扫窟
来触发按钮上的'click \'然后检查值,而不是直接调用您的回调函数,当单独调用该函数时,回调函数的作用域不限于按钮的
。
烷刨画颠离
例如
所以我会尝试