Thursday, April 30, 2015

4/29/15 and 4/30/15 - Restructuring Tests

I hit a brick wall and spent about 30 minutes trying to figure out why my before() hook wasn't calling before my BookStorageManager tests (I was getting undefined variables). It turns out that I had forgotten to surround my test code in an it() statement, and so the test code was being evaluated at the wrong time, before the before() method had been called. This was really hard to debug because I was convinced that the problem was with the before statement itself, and I didn't think to check whether the code in the test was set up properly.

Also, I think I was wrong (again) in one of my blog posts.  It seems like calling mock.verify() on a mock doesn't actually clear expectations, so if you have multiple expectations dealing with the same mocked method, you will run into problems.  So I think that doing

beforeEach(function () { mock = sinon.mock(obj); });
is necessary when expecting multiple calls to the same method across different tests.

I also realized that the BOOK_OBJECT constant we use in our tests (for example) wouldn't reflect any new changes we made to how Book objects are generated from librivox json (for example, changing the id from a string to an integer had to be manually done in BOOK_OBJECT alongside app.js). I fixed this somewhat by setting book object to var BOOK_OBJECT = new Book({json: WEB_RESP.book_json}); so that our test book object will be generated from our json each time the tests are run. I'm still a bit fuzzy on this aspect of testing, so this might not be best practice. I'll check with Kevin.

I worked on cleaning up our tests. I moved the tests for each object into separate files. I also moved all the simulated web response objects into their own object so that the global namespace didn't get start getting polluted (even though it was only three things, I like it better this way). So now the responses can be gotten with WEB_RESP.audio_blob WEB_RESP.book_json WEB_RESP.book_xml.

No comments:

Post a Comment