简体   繁体   English

在验收测试中调试Ember CLI Mirage

[英]Debugging Ember CLI Mirage in an acceptance test

I'm trying to setup a mock server in for my Ember CLI (1.13.1) acceptance test with Ember CLI Mirage . 我正在尝试使用Ember CLI Mirage为我的Ember CLI(1.13.1)接受测试设置一个模拟服务器。 I'm stuck on how to debug the setup and actually test the model data available in the view. 我一直在研究如何调试设置以及实际测试视图中可用的模型数据。

I tried adding a console log statement inside my mirage route: 我尝试在海市rage楼路线中添加控制台日志语句:

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

Which tells me that the mirage route is called and there should be three users present. 这告诉我已调用了幻影路由,并且应该有三个用户在场。 But my test is still failing. 但是我的测试仍然失败。 How do I check what is in the store in my acceptance test or in my template? 如何在验收测试或模板中检查商店中的内容?

tests/acceptance/users/index-test.js 测试/接受/用户/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: expected 0 to equal 3 AssertionError:预期0等于3

app/mirage/config.js 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 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 应用程序/路由/用户/index.js

import Ember from 'ember';

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

app/templates/users/index.hbs 应用程序/模板/用户/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>

I usually start by looking in the Data tab of the Ember Inspector to see if any models were added to the store. 我通常首先查看Ember Inspector的“数据”选项卡,以查看是否有任何模型添加到商店中。

If you're on 1.13 you're probably using the JSON API adapter and will need to do just a bit more work in your mirage route handler, like returning the object under a data key with a type. 如果您使用的是1.13,则可能正在使用JSON API适配器,并且将需要在Mirage路由处理程序中做更多的工作,例如在带有类型的数据键下返回对象。

For example, it might look like this: 例如,它可能看起来像这样:

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

Note that your factories are only for seeding Mirage's db. 请注意,您的工厂仅用于播种Mirage的数据库。 So with the route above, you'd now be able to use a factory like the one you have defined in your question 因此,使用上述路线,您现在就可以使用您在问题中定义的工厂。

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

and then when you booted your app and it made a GET request to /users , the data should be returned and properly deserialized by Ember Data. 然后,当您启动应用程序并向/users发出GET请求时,数据应返回并由Ember Data正确反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ember CLI Mirage:2级侧载响应 - Ember CLI Mirage: 2 level sideloaded response 在Ember验收测试中出错 - Getting error in Ember Acceptance test 使用ember-cli-mirage失败的Ember destroyRecord(或deleteRecord然后保存) - Ember destroyRecord (or deleteRecord then save) using ember-cli-mirage failing 如何使Ember Cli Mirage与Ember Simple auth一起使用 - How to make Ember Cli Mirage to work with Ember Simple auth 使用Ember(cli)如何获得等待承诺的验收测试? - Using Ember (cli) how do I get an acceptance test to wait for a promise? Ember-cli验收测试超时等待click(); qunit error:Uncaught(in promise)错误:pushFailure() - Ember-cli acceptance test times out on await click(); qunit error: Uncaught (in promise) Error: pushFailure() 在余烬气候0.2.x中重构模拟响应 - Refactoring mock response in ember cli mirage 0.2.x ember-cli-mirage重定向在Mirage中注入的socket.io客户端 - ember-cli-mirage redirects socket.io client, which is injected in mirage Ember.js - 使用ember-cli-mirage作为假模型时未找到模型待办事项 - Ember.js - Model todo not found when using ember-cli-mirage for a fake model 在 ember-cli-mirage(或任何 JavaScript)中生成 jwt web auth 令牌以在您的 ember 应用程序中使用 - Generate jwt web auth tokens in ember-cli-mirage (or any JavaScript) for use in your ember app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM