Database Fixtures for Integration Tests

When you write integration tests, they will run in an isolated instance of your app with an empty database. Usually you want to create some sample data in your database that you use in your integration tests. This can be achieved with database fixtures. It's a script that sets up your database for integration tests.

Server integration test fixtures

Use this script to get started: https://gist.github.com/Sanjo/edbc0c1848de726b78d5

Copy it to a file in /tests/jasmine/server/integration/ for server integration.

Client integration test fixtures

Create file /tests/jasmine/client/integration/database-fixture.js with this content:

Meteor.call('clearDB', function(){
  Meteor.call('loadFixtures');
});

Add to server side script with code based on this script: https://gist.github.com/qnub/97d828f11c677007cb07

Fixtures via Package

Another pattern for creating fixtures is to do the following inside your app:

meteor create --package fixtures

Then modify the package.js file to set the debugOnly flag to true like this:

Package.describe({
  name: 'fixtures',
  version: '0.0.1',
  debugOnly: true,
  // ...
});

The debugOnly flag instruct Meteor not to bundle this package when building, which is how you
ensure this package does not make it to production. You can now define all your fixtures in this
package.

Now add some Meteor methods to the package that will create your fixture data. You can call this Meteor methods from your tests.

fixtures.js:

var createUser = function (userData) {
  var user = Meteor.users.findOne({username: userData.username});

  if (!user) {
    var userId = Accounts.createUser(userData);
    user = Meteor.users.findOne(userId);
  }

  return user;
};

Meteor.methods({
  'fixtures/users/create': createUser
});

You can find a full example of a fixtures package here.