对于旧版本的ember,this is the accepted answer for mocking a service in an acceptance test
使用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)为什么没有记录?余烬docs provide excellent documentation on stubbing services in components。
b)是否因为在验收测试中模拟服务而不鼓励?为什么呢?
答案 0 :(得分:1)
来自the guide:
验收测试用于测试用户交互和应用程序 流。测试以与a相同的方式与应用程序交互 用户可以通过填写表单字段和单击来执行操作 纽扣。验收测试确保项目中的功能 基本上是功能性的,并且在确保核心方面很有价值 一个项目的功能没有退化,并且该项目的目标 正在接受。
没有“明确”提及,但我理解“验收测试与实际应用程序的行为相同”。所以你不应该嘲笑某事。