Using Fakes from Ember-Sinon-Qunit

Derek Gray
2 min readJan 3, 2019

Updated/Edited 2019-Jan-6: Found a few moments to submit a PR to ember-sinon-qunit, which has since been accepted. Added this info and an example.

I do prefer to use the ember-sinon-qunit addon because of the automatic cleanup on each test. This means you also reference this.stub() rather than an imported sinon.stub(). Well, at v3.3.0, I tried using the replace() and fake functions this way … but they were undefined! Instead you must reference them instead via this.sandbox.*. So this.sandbox.fake.* and this.sandbox.replace(...).

The addon has been around much longer than Sinon v5 has, so there were no references to fake in the README. (There is also another project, https://github.com/scalvert/ember-sinon-sandbox, where you use this.sandbox.* for everything. The setup of that one is more in line with the latest patterns of using the test hooks.)

At the prompting of the author (see comments to original draft below) I submitted a PR so the next release will not have this discrepancy. I’ve updated the README with the list of exposed functions and updated the example. Here’s an extra example using fake and replace to return a canned result from the ember-data store (this was a real use-case of mine because Mirage doesn’t support store.query’s parameters so it will return all records while I needed it to return none):

Contrived example using sinon’s `replace` and `fake`.

Hope this helps you if you are more in the habit of using the newer Fakes API over the older stub/spy functions.

--

--