Server unit mode

Advanced features of the server unit mode

Mocks

Mocking Meteor

This package ships with mocks for the Meteor API. You can mock the Meteor API in your tests with:

beforeEach(function () {
  MeteorStubs.install();
});

afterEach(function () {
  MeteorStubs.uninstall();
});

This is done automatically for server unit tests. To disable on the server for certain packages set the environment variable JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS. For example

export JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS=dburles:factory 

You need to do it yourself for your client tests if you want to write
unit tests that run in the browser.

Mocking objects

You can mock any object with the global helper function mock.
This will avoid unwanted side effects that can affect other tests.
Here is an example how to mock the Players collection of the Leaderboard example:

beforeEach(function () {
  mock(window, 'Players');
});

This will mock the Players collection for each test.
The original Players collection is automatically restored after each test.

Creating more complex mocks

You can also create mocks manually. I would suggest to use the following pattern:

Create a mock service with a method install and uninstall (example for Meteor)

  • install: Saves the original object and mocks it
  • uninstall: Restores the original object

This pattern allows you to enable mocks only for specific tests and have a clean and independent mock for each test.