-
Notifications
You must be signed in to change notification settings - Fork 32
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
.
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.
'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);
});
});
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();
// 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();
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.
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
.
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.
- They are all in Mozilla B2G nightly repository
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
Clone the repository
git clone https://github.com/mozilla-b2g/gaia.git
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 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
Assuming you have installed Gaia, as described above. You can use the following tasks:
grunt push
grunt push-clean
grunt push-hard