对于旧版本的 ember,这是在验收测试中模拟服务的公认答案
使用 Ember 2.7.0,我无法按照上面的答案调用 startApp。但是,经过一些快速测试,这似乎在注入服务时工作得很好。
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(), '/');
});
});
我错过了什么吗?我有以下疑问:
a) 为什么没有记录?Embers 文档提供了关于组件中存根服务的优秀文档。
b) 是因为不鼓励在验收测试中模拟服务吗?为什么?