Tuesday, April 28, 2015

4/28/15 - Strings and Integers!

I think I finally figured out the strange test failings we've been getting on our tests when using sinon to expect method calls with arguments. We would get failure messages like:
Unexpected call: test(1234)
            Expected test(1234) once (never called)
on our tests, where the first line is what was actually called, and the second line is what was expected.  They appear identical.

Here's some tests that illustrate what the problem is:
// FAILING TEST
describe('#test', function () {
it('tests args', function () {
storageMock.expects('test').once().withExactArgs(1234);
storageManager.otherFunctionThatCallsTest(chapter_obj.id); // should call #test with args: 1234
storageMock.verify();
});
});
/* Console Output:
* Unexpected call: test(1234)
* Expected test(1234) once (never called)
*/
// PASSING TEST
describe('#test', function () {
it('tests args', function () {
storageMock.expects('test').once().withExactArgs('1234');
storageManager.otherFunctionThatCallsTest(chapter_obj.id); // should call #test with args: 1234
storageMock.verify();
});
});
/* Console Output:
* ... SUCCESS!
*/

Can you spot the difference? It turns out that the id field in the book object is actually a string, and not an integer!  Obviously the error output is just converting all the arguments to strings before outputting, and so there's no way to tell what type they are just by looking at the output.  To fix this, I'll add a test to the Book object to ensure that it converts strings passed in to integers and update the relevant lines in app.js.

It really annoys me that such a silly thing took up so much of my time though.

No comments:

Post a Comment