Following my testing described here, I wanted to verify that Meteor interacts with my Routers correctly.
The Meteor.Collection object exposes methods for querying the MongoDB collection as described in the docs. I want to test if my index callback will retrieve all the items it should.
Add the new module we need:
Here’s how to use the spy:
Now, let’s write our test:
1 2 3
This will fail, since we need to first modify our Router to accept a collection as an argument and then call find on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
This should pass, but yet doesn’t. Why? Thinking about the assumption our test is making when using the a Backbone router, it likely depends on Backbone initialization. We can try this in our test, also:
1 2 3
This doesn’t work either because Backbone assumes we are in the browser when accessing window. I’m pretty sure this is not the approach I want. We don’t need to add a dependency on Backbone’s internal behavior to test a method of my Router. My previous test verifies that the root path will call an index method, so now I only need to call that index method and validate that behavior. This has the added benefit of simplifying the test.
Here’s the final version of my tests:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
A bit roundabout, but in the end what I was trying to accomplish. Source here.