This addon integrates Sinon & Ember-QUnit via Ember-Sinon, as inspired by Sinon-QUnit.
Why not simply use Ember-Sinon? Two reasons:
- Ember-Sinon does not handle cleanup of Ember-QUnit tests. Ember-Sinon-QUnit provides a test method that wraps Ember-QUnit while making each test callback a sandboxed Sinon environment. All spies/stubs created via the sandbox will be automatically restored to their original methods at the end of each test.
- Sinon is a framework-agnostic library; as such, Ember-Sinon should be as well. This addon exists to enable Ember-Sinon to remove its QUnit specific functionality, making it easier to utilize Ember-Sinon with other addons like Ember-CLI-Mocha, for example.
ember install ember-sinon-qunit
Import Ember-Sinon-QUnit's test
method into your tests in place of Ember-QUnit's test. This creates a Sinon sandbox
around that test via Sinon's test
API. Then, you can access Sinon's spy
, stub
, mock
, and sandbox
methods
via this
within the test callback:
import { moduleFor } from 'ember-qunit';
import test from 'my-app/tests/ember-sinon-qunit/test';
//import test from 'dummy/tests/ember-sinon-qunit/test'; => In case of addons
moduleFor('route:foo', 'Unit | Route | foo');
test('fooTransition action transitions to bar route', function (assert) {
const route = this.subject();
const stub = this.stub(route, 'transitionTo');
route.send('fooTransition');
assert.ok(stub.calledOnce, 'transitionTo was called once');
assert.ok(stub.calledWithExactly('bar'), 'bar was passed to transitionTo');
});
That's it! You can use this test
method in place of all Ember-QUnit tests if you want, without any
loss of functionality. Or, you can import them both into the same test to be used only when you need Sinon:
import { moduleFor, test } from 'ember-qunit';
import sinonTest from 'my-app/tests/ember-sinon-qunit/test';
//import sinonTest from 'dummy/tests/ember-sinon-qunit/test'; => In case of addons
git clone [email protected]:elwayman02/ember-sinon-qunit.git
cd ember-sinon-qunit
npm install
bower install
npm test
(Runsember try:testall
to test your addon against multiple Ember versions)ember test
ember test --server