CodeChimp
CodeChimp

Reputation: 8154

Running code only for tests using Jasmine package

I am using the sanjo:jasmine and velocity:html-reporter packages in my app to try and implement some unit and integration testing. Using this tutorial as a guide, I have a few unit tests and a couple integration tests done. What I am not able to figure out is how to get code to run in the "test" environment that is not part of a unit test or integration test, but needs to run prior to the tests and only for the tests.

What I am trying to solve is that I need some dummy users created for testing, but I do not want them in my production app. Sort of like an "init" phase where you can build the mockups and insert any data you need. Is there a way to accomplish this?

Upvotes: 1

Views: 437

Answers (3)

Joaquín L. Robles
Joaquín L. Robles

Reputation: 6494

If you're using sanjo:jasmine you can insert data into the mirrored db before writing your specs (after describe and before it clauses) and this data would be available for all specs.

Also, you may use beforeEach() in order to provide data for each specs, and then you can delete it using afterEach().

Here you can find more info.

Upvotes: 1

azium
azium

Reputation: 20614

I've been using mike:mocha and as long as your specs are written inside a folder called tests (and then client / server, respectively) then Velocity puts data in velocity specific collections. I run the same Meteor method I use to insert a document in my main app, but velocity knows to put it in the mirrored version.

Upvotes: 0

Chip Castle
Chip Castle

Reputation: 2192

  1. I would recommend that you create some seed or fake data for your tests using factories.

    I would recommend that you try the following packages:

    • anti:fake - Fake text and data generator for Meteor.js
    • dburles:factory - A package for creating test data or for generating fixtures.
  2. You can install these packages using this command:

    meteor add anti:fake dburles:factory

  3. Create your factory data for the test environment only.

I'd create a file called server/seeds.js with the following content:

Meteor.startup(function() {

  Factory.define('user', Users, {
      username: "test-user",
      name: "Test user",
      email: "[email protected]"
      // add any other fields you need
  });

  var numberOfUsers = 10;

  // Ensure this is the test environment
  if (process.env.NODE_ENV === 'test') {

    // Create the users from the factory definition
    _(numberOfUsers).times(function(n) {
      Factory.create('user');
    });
  }
});

You can follow this Factory approach for any data, not just Users.

If your Users need to login, such as when you're using accounts:base, then I would consider an alternative approach to using Factory data:

var email = "[email protected]";
var password = "secret";
var name = "Test user";

Accounts.createUser({email: email, password: password, profile: {name: name}});

Please see Accounts.createUser in the Meteor docs for more details.

Upvotes: 2

Related Questions