【问题标题】:Debugging Ember CLI Mirage in an acceptance test在验收测试中调试 Ember CLI Mirage
【发布时间】:2015-10-25 10:37:48
【问题描述】:

我正在尝试使用 Ember CLI Mirage 为我的 Ember CLI (1.13.1) 验收测试设置一个模拟服务器。我不知道如何调试设置并实际测试视图中可用的模型数据。

我尝试在 mirage 路由中添加控制台日志语句:

this.get('/users', function(db){
  console.log(db.users);
  return db.users;
});

这告诉我调用了 mirage 路由并且应该有三个用户在场。但我的测试仍然失败。如何在验收测试或模板中检查商店中的内容?

tests/acceptance/users/index-test.js

/* jshint expr:true */
import {
  describe,
  it,
  beforeEach,
  afterEach
} from 'mocha';
import { expect } from 'chai';
import Ember from 'ember';
import startApp from 'tagged/tests/helpers/start-app';

describe('Acceptance: UsersIndex', function() {
  var application;
  var users;

  beforeEach(function() {
    application = startApp();
    users = server.createList('user', 3);
  });

  afterEach(function() {
    Ember.run(application, 'destroy');
  });

  it('can visit /users/index', function() {
    visit('/users');
    andThen(function() {
      expect(currentPath()).to.equal('users.index');
    });
  });

  it('lists the users', function(){
    visit('/users');
    andThen(function() {
      users = server.createList('user', 3);
      expect(find('.user').length).to.equal(3); // fails
    });
  });
});

AssertionError:预期 0 等于 3

app/mirage/config.js

export default function() {
  /*
    Config (with defaults).

    Note: these only affect routes defined *after* them!
  */
  this.namespace = '/api/v1';    // make this `api`, for example, if your API is namespaced
  // this.timing = 400;      // delay for each request, automatically set to 0 during testing

  this.get('/users');
}


// You can optionally export a config that is only loaded during tests
export function testConfig() {
  this.timing = 1;
}

app/mirage/factories/user.js

import Mirage, {faker} from 'ember-cli-mirage';
export default Mirage.Factory.extend({
  email: function(){ return faker.internet.email(); }
});

app/routes/users/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.findAll('user');
  }
});

app/templates/users/index.hbs

<h2>Users</h2>

<table>
  <thead>
    <tr>
      <th>Actions</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>

  {{#each model as |user|}}
    <tr class="user">
      <td class="actions"><a href="#">Show</a></td>
      <td class="email">{{ user.email }}</td>
    </tr>
  {{/each}}
  </tbody>
</table>

【问题讨论】:

    标签: javascript ember.js ember-cli ember-cli-mirage


    【解决方案1】:

    我通常首先查看 Ember Inspector 的“数据”选项卡,看看是否有任何模型添加到存储中。

    如果您使用的是 1.13,您可能正在使用 JSON API 适配器,并且需要在 mirage 路由处理程序中做更多的工作,例如在具有类型的数据键下返回对象。

    例如,它可能看起来像这样:

    this.get('/users', function(db){
      return {
        data: db.users.map(u => ({
          id: u.id,
          type: u.type,
          attributes: _.omit(u, ['id', 'type'])
         }))
      };
    });
    

    请注意,您的工厂仅用于播种 Mirage 的数据库。因此,通过上述路线,您现在可以使用您在问题中定义的工厂

    // mirage/scenarios/default.js
    export default function(server) {
      server.createList('user', 10);
    });
    

    然后当您启动应用并向/users 发出 GET 请求时,Ember Data 应返回数据并对其进行正确反序列化。

    【讨论】:

    • 是的,我正在使用JSONAPIAdapter。有道理,因为我之前使用了 Sinon.fakeServer 并根据 JSONAPI 设置响应。
    • 感谢您的快速回复!
    • 所以我需要获取工厂记录并通过JSONAPISerializer之类的方式传递它们?
    • 再次感谢一百万,我冒昧地编辑了您的答案,并进行了一些小调整,以创建符合 JSONAPI 的响应对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2015-04-09
    • 2019-10-13
    相关资源
    最近更新 更多