Skip to content
This repository was archived by the owner on May 11, 2020. It is now read-only.

Testing

Michael Moroni edited this page Jul 14, 2015 · 2 revisions

Unit Testing

We are using mocha for unit testing. We are using the BDD mode of mocha. We complement it with chaijs for expectations (e.g. expect(variable).to.equal("pepe")).

Test cases are stored in test/spec and should follow the same directory structure as the matching javascript files.

The test suite is defined in test/index.html.

Running tests

Run tests in command-line using grunt test or run them in a web page using grunt server:test. The latter method allows for easily viewing the results, choosing one particular test to repeat etc.

Example test case using mocha:

'use strict';
describe('models/auth tests', function () {
  var AuthModel;

  before(function (done) {
    require([
      'models/auth'
    ], function (model) {
      AuthModel = model;
      done();
    });
  });

  it('should initialize with empty parameters', function () {
    var auth = new AuthModel();
    expect(auth.get('loggedIn')).to.equal(null);
    expect(auth.get('userId')).to.equal(null);
    expect(auth.get('password')).to.equal(null);
  });
});

Example stub usage:

var credStub = sinon.stub(authStorage, 'load');
credStub.returns({userId: 'chuck', password: 'pass'});
//
// now, any call to authStorage.load will use the stub
// ...
//
credStub.restore(); // restore normal authStorage function

### Example mock usage:

// we use prototype because we want to intercept all instances
var mock = sinon.mock(AuthModel.prototype);
mock.expects('_navigate').withArgs('index').once();

var auth = new AuthModel();
auth.set({ loggedIn: true });

// Always call verify() to run expectations
mock.verify();

Example mocking the clock to travel to the future:

// Initialize the clock stub
var clock = sinon.useFakeTimers();

this.view.render();

// Travel 16 seconds to the future
clock.tick(16000);

// Do the expectations
expect(this.view.$el.find('h3')).to.have.length(0);

// Always restore the clock!
clock.restore();

When to use what

Mocks documentation is in Sinonjs site.

  • Use expect to verify that return values comply with what is required
  • Use stubs when you want to return static objects or values for some calls.
  • Use mocks when you want to test the behaviour of a method.

Notes about testing

Use of globals is disallowed in tests by default. This is enforced by mocha and will be triggered during testing. Configuration for allowed globals is in test/index.html.

Testing with simulator

We are working with a target of v1_0_1 for the Gaia user interface (the interface that you see on the device). The standalone simulator allows you to choose a specific version so it's ideal for us.

Install the simulator

We will be using the latest b2g18_v1_0_1 (Gecko v18, Gaia version 1.0.1), get it from mozilla

  • Download and install the package for your system

Install gaia

Clone the repository

  git clone https://github.com/mozilla-b2g/gaia.git

Create local configuration file

Make sure to have a grunt.local.json file that has the correct paths for your B2G installation and your gaia installation.

There is a sample file that you should copy. Your local file is not to be uploaded to the git repo.

Launch the simulator

Launch the simulator using:

  grunt simulate

If you want to have some pre-generated contacts, copy contacts.json.sample to contacts.json before executing:

  grunt simulate

To remove the generated contacts:

  grunt clean-contacts

Internally, the 'simulator' task will do:

  • Create a soft link from the GAIA apps home to the DIST directory of the application. This means you don't have to copy any files there.
  • Build the application and copy all files to DIST directory
  • Kill the simulator if it's already running
  • Launch the simulator with the correct profile

Testing with the device

Assuming you have installed Gaia, as described above. You can use the following tasks:

Install only your application

  grunt push

Clean all applications and install again (make-gaia)

  grunt push-clean

Reset the device properties and applications and install

  grunt push-hard