使用 Qunit 测试返回的函数
我目前正在学习单元测试以及如何使用 QUnit,并认为最好的方法是使用我编写的一个小型 jQuery 插件。
在插件中,我使用缓动插件中的方程扩展了缓动对象,如下所示:
$.extend( $.easing, {
'ease-in': function (x, t, b, c, d) {
return c*(t/=d)*t*t + b;
},
'ease-out': function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t + 1) + b;
},
});
现在我尝试在 QUnit 测试中使用它:
equal(jQuery.easing['ease-in'],
function (x, t, b, c, d) {return c*(t/=d)*t*t + b;},
'ease-in returns correct function');
但它失败了...我是否遗漏了某些内容,或者我得到了错误的结尾粘在某个地方?
I'm currently learning unit testing and how to use QUnit and thought that the best way to do this would be using a small jQuery plugin that I've written.
Within the plugin I've extended the easing object using the equations from an easing plugin like so:
$.extend( $.easing, {
'ease-in': function (x, t, b, c, d) {
return c*(t/=d)*t*t + b;
},
'ease-out': function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t + 1) + b;
},
});
Now I try to use this within a QUnit test:
equal(jQuery.easing['ease-in'],
function (x, t, b, c, d) {return c*(t/=d)*t*t + b;},
'ease-in returns correct function');
and it fails... am I missing something or have I got the wrong end of the stick somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是单元测试(通常)完成的方式——没有理由测试方法的代码是否等于测试代码(你知道它等于!)。单元测试的目的是确保结果相同。该方法如何计算结果并不重要。
所以,你的测试应该是这样的:
That's not how unit testing is (usually) done -- there's no reason to test whether a method's code equals the test code (you know it does!). What unit testing is for is to make sure that the results are equal. How the method calculates the result is not important.
So, your test should look something like this: