mattholl
mattholl

Reputation: 180

Using Qunit to test a returned 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?

Upvotes: 2

Views: 454

Answers (1)

JJJ
JJJ

Reputation: 33153

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:

var easeIn = jQuery.easing['ease-in'];
equal(
    easeIn( 1, 2, 3, 4, 5 ),
    123  // or whatever the result should be
);

Upvotes: 2

Related Questions