9

This isn't so much a question of technology; I see that a lot of people are using Mocha, which looks neat, so I'll try it.

What I'm wondering is how people deal with testing things like models that are tied to Meteor collections. Would you use sinon to mock the database records? Are there framework files that you would load using require so that you could use Meteor.Collection?

Edit

To be more specific, I'm writing an app that uses several Minimongoid models. If you've seen one of these, you know that every model's @_collection property is a new Meteor.Collection(...).

One of the main advantages to having a model instead of a database record is that you can attach behavior to it, and that behavior is what I'm trying to test.

So let's say you have a game involving two pieces on a board. When the player clicks a piece, we want to show all legal moves on the board by highlighting the squares. The piece has a method that determines whether or not a given location constitutes a legal move, based on how that piece moves and whether there are other pieces in its way; determining whether there are pieces in the way probably requires a database query:

class Piece extends Minimongoid
  @_collection: new Meteor.Collection('pieces')

  @find: (selector = {}, options = {}) ->
    document = @_collection.findOne(selector, options)
    if document
      model = new @(document)
      _.extend(model, model.attributes)

  @where: (selector = {}, options = {}) ->
    @_collection.find(selector, options).map (record) =>
      model = new @(record)
      _.extend(model, model.attributes)

class Bishop extends Piece
  @code: "bishop"

  isLegalMove: (location) ->
    @isOnMyPath(location) && @noPiecesInMyWay(location)

  noPiecesInMyWay: (location) ->
    _.all Piece.where({ color: @otherColor() }), (piece) -> 
      !piece.isOnMyPath(location) || (piece.location == location && piece.color == @otherColor())

So if I load this class with a testing framework, I really only see two options for testing this code:

  1. Mock the @_collection object with something like sinon
  2. Have Meteor (or parts of it) loaded by the test framework so that my models have access to Meteor.Collection

Update

About a year later I came back to this and tried out some of the suggested approaches to testing.

I took a look at RTD, which seemed to be the most complete of all the solutions available, but I could not get it to run.

I also had a look at Laika, but really did not care for the syntax. I want a BDD style syntax, and what Laika offers is rather cryptic looking in comparison.

After a bit of trial and error, however, I was able to get meteor-mocha-web to work, and so far I'm very pleased with it. It runs your app and pulls in your test files, so there's no need to mock any part of the framework. It's far from perfect of course, but it's the first thing I was able to get running and it has the syntax I prefer, so it wins. Thanks to jagill for the answer!

Samo
  • 8,202
  • 13
  • 58
  • 95
  • While I agree that tests for large code base is a priority, I am slightly confused about tests on Meteor because, I believe it embraces a philosophy for MVP. Do you think that tests based on Meteor are extremely essential? Are you planning to build a large scale app using Meteor to be put into production? – Prashant Mar 03 '13 at 11:06
  • While it kind of feels like you're baiting me into a flame war about the merits of testing, I'll see if I can bite the hook without pushing the button. It doesn't matter whether the app is large scale and going to production; what matters is that I want to discover my bugs as soon as I create them, since they're a lot easier to fix that way. In my case, I'm using several Minimongoid models that have complicated logic dictating their behavior; there's a lot of opportunities for something to go wrong, and it's easier to drive my code using tests than by clicking through the app. – Samo Mar 03 '13 at 18:56
  • Not only is it easier, but it's also faster and more effective. – Samo Mar 03 '13 at 18:56
  • Sorry if you felt 'baiting'. No offense intended. Now that you have extended your question, things are clear. Thank you. – Prashant Mar 03 '13 at 20:12
  • You might want to check http://stackoverflow.com/questions/12987525/meteor-test-driven-development – Prashant Mar 03 '13 at 20:18

1 Answers1

3

We've run into similar problems when trying to test our app, MadEye.io. There are a lot of moving parts and edge cases, so an automated suites were essential. We've built a package meteor-mocha-web to enable testing in the Meteor context, including Session, Meteor.Collections, Deps, etc. We also gave a Lightning Talk at Meteor's Devshop 0 explaining it briefly.

It's early stages, but it's been invaluable to us, and it's covering more use-cases as people need them.

jagill
  • 692
  • 4
  • 12