3

对于旧版本的 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) 是因为不鼓励在验收测试中模拟服务吗?为什么?

4

1 回答 1

1

指南

验收测试用于测试用户交互和应用程序流程。测试以与用户相同的方式与应用程序交互,通过填写表单字段和单击按钮等操作。验收测试确保项目中的特性基本正常运行,并且在确保项目的核心特性没有退化以及项目的目标得到满足方面是有价值的。

没有“明确”提及,但我理解“验收测试与实际应用程序的行为相同”。所以你不应该嘲笑什么。

于 2016-09-11T08:26:14.337 回答