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 setupTest
function can be used to setup a unit test for any kind
of "module/unit" of your application that can be looked up in a container.
For example, the following is a unit test for the SidebarController
:
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';
describe('SidebarController', function() {
setupTest('controller:sidebar', {
});
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.
Component Test Modules
The setupComponentTest
function is specifically designed to test components
and provides additional render
and $
helpers within a test's context.
import { describe, it } from 'mocha';
import { setupComponentTest } from 'ember-mocha';
describe('GravatarImageComponent', function() {
setupComponentTest('gravatar-image', {
});
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 setupModelTest
function can be used to test Ember Data models and
provides an additional store
helper within a test's context.
import { describe, it } from 'mocha';
import { setupModelTest } from 'ember-mocha';
describe('Contact', function() {
setupModelTest('contact', {
needs: []
});
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.