For older versions of ember, this is the accepted answer for mocking a service in an acceptance test
With Ember 2.7.0, I am not able to call startApp as per the answer above. However, upon some quick tests, this appears to work just fine in injecting a service.
import Ember from 'ember';
import { module, test } from 'qunit';
let speakerMock = Ember.Service.extend({
speak: function() {
console.log("Acceptance Mock!");
}
});
module('Acceptance | acceptance demo', {
beforeEach: function() {
// the key here is that the registered service:name IS NOT the same as the real service you're trying to mock
// if you inject it as the same service:name, then the real one will take precedence and be loaded
this.application.register('service:mockSpeaker', speakerMock);
// this should look like your non-test injection, but with the service:name being that of the mock.
// this will make speakerService use your mock
this.application.inject('controller', 'speakerService', 'service:mockSpeaker');
}
});
test('visit a route that will trigger usage of the mock service' , function(assert) {
visit('/');
andThen(function() {
assert.equal(currentURL(), '/');
});
});
Am I missing something? I have doubts about:
a) Why this is not documented? Embers docs provide excellent documentation on stubbing services in components.
b) Is it because it is discouraged to mock services inside an acceptance test? Why?