Ember Mocha
Ember Mocha simplifies unit testing of Ember applications with Mocha by
providing Mocha-specific wrappers around the helpers contained in
ember-test-helpers.
Installation
Installation with Ember CLI
In order to use Ember Mocha with Ember CLI,
please follow the instructions for
ember-cli-mocha.
Standalone Installation
Ember Mocha can also be installed with bower and used directly in any Ember
project:
$ bower install ember-mocha
You can then choose to include the global (ember-mocha.js
) or AMD
(ember-mocha.amd.js
) build when running your tests.
Usage
Setting the Resolver
You'll typically want to set a single resolver for your test suite:
import resolver from './helpers/resolver';
import { setResolver } from 'ember-mocha';
setResolver(resolver);
If you want to use multiple resolvers in your test suite, you can also
call setResolver
in the beforeSetup
callback of your test modules.
Test Modules
The describeModule
helper can be used to declare a module that can be used
to test any "unit" of your application that can be looked up in a container.
For example, the following is a unit test for the SidebarController
:
import {
describeModule,
it
} from 'ember-mocha';
describeModule(
'controller:sidebar',
'SidebarController',
{
},
function() {
it('exists', function() {
var controller = this.subject();
expect(controller).to.be.ok;
});
}
);
The subject is specified as controller:sidebar
, which is the key that will
be used to look up this controller in the isolated container that will be
created for this test.
The title, 'SidebarController'
, will appear in the test runner and is not
strictly necessary. It defaults to the subject if omitted.
Component Test Modules
The describeComponent
helper extends describeModule
and provides
additional render
and $
helpers within a test's context.
import {
describeComponent,
it
} from 'ember-mocha';
describeComponent(
'gravatar-image',
'GravatarImageComponent',
{
},
function() {
it('renders', function() {
var component = this.subject();
expect(component._state).to.equal('preRender');
this.render();
expect(component._state).to.equal('inDOM');
});
}
);
Model Test Modules
The describeModel
helper also extends describeModule
and provides an
additional store
helper within a test's context.
import {
describeModel,
it
} from 'ember-mocha';
describeModel(
'contact',
'Contact',
{
needs: []
},
function() {
it('exists', function() {
var model = this.subject();
expect(model).to.be.ok;
});
}
);
Asynchronous Testing
Mocha supports asynchronous testing with both promises and callbacks.
describe('it', function() {
it('works with asynchronous tests using callbacks', function(done) {
setTimeout(function() {
expect(true).to.equal(true);
done();
}, 10);
});
it('works with asynchronous tests using promises', function() {
return new Ember.RSVP.Promise(function(resolve) {
setTimeout(function() {
expect(true).to.equal(true);
resolve();
}, 10);
});
});
});
Contributing
Contributions are welcome. Please follow the instructions below to install and
test this library.
Installation
$ npm install
Testing
In order to test in the browser:
$ npm start
... and then visit http://localhost:4200/tests.
In order to perform a CI test:
$ npm test
Copyright and License
Copyright 2014 Switchfly
This product includes software developed at
Switchfly (http://www.switchfly.com).
NOTICE: Only our own original work is licensed under the terms of the Apache
License Version 2.0. The licenses of some libraries might impose different
redistribution or general licensing terms than those stated in the Apache
License. Users and redistributors are hereby requested to verify these
conditions and agree upon them.