简体   繁体   English

如何修改/更改我的测试文件中的 ember mirage 响应?

[英]How to modify/change the ember mirage response in my tests file?

The application uses ember-cli-mirage to mock the data and uses specific endpoint to get the specific response.该应用程序使用 ember-cli-mirage 模拟数据并使用特定端点获取特定响应。 Mocking data and showing the content to the templates works fine. Mocking 数据并将内容显示到模板工作正常。

Problem: I can't modify the response of this endpoint GET /foos in my test file.问题:我无法在我的测试文件中修改此端点GET /foos的响应。

/mirage/config.js

export default function () {
  this.namespace = '/api';

  let foos = {
    foos: [
      {
        id: 1,
        name: 'foo-2',
        description: 'foo description'
      },
    ],
  };

  this.get('/foos', function () {
    return foos;
  });
}

tests/acceptance/foo-test.js

import { module, test } from 'qunit';
import { visit, currentURL, click, find, findAll } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Acceptance | foo', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  test('visiting /foo', async function (assert) {
    this.server.get('/foos', () => {
      return new Response(
        200,
        {
          foos: [
            {
              id: 20,
              name: 'foo-3'
            }
          ]
        },
      );
    })

    await visit('/foo');
    assert.equal(currentURL(), '/foo');
  });
});

Question: How to modify the response of this endpoint GET /foos inside my test file?问题:如何在我的测试文件中修改此端点GET /foos的响应? I want my test file to have a different response我希望我的测试文件有不同的响应

We just tried your code in a new demo app and I found 2 issues with it.我们刚刚在一个新的演示应用程序中尝试了您的代码,我发现了 2 个问题。

Firstly you are returning a new Response from your server handler which will not work on its own.首先,您从服务器处理程序返回一个新的Response ,它不能单独工作。 Without importing anything you have ended up using the browser's Response object which is not what MirageJS is expecting.如果不导入任何内容,您最终会使用浏览器的响应 object ,这不是 MirageJS 所期望的。 To fix this you need to first import Response from MirageJS and it will then be using the right object:要解决此问题,您需要先从 MirageJS 导入Response ,然后它将使用正确的 object:

import { Response } from 'miragejs';

The second issue is that you are missing a parameter for the Response constructor.第二个问题是您缺少Response构造函数的参数。 It expects 3 parameters: the status code, a headers object, and the data.它需要 3 个参数:状态代码、标头 object 和数据。 You have only passed the code and the data, which means that it is considering your data object to be the headers object.您只传递了代码和数据,这意味着它正在将您的数据 object 视为标头 object。

Here is a corrected version of your test code:这是您的测试代码的更正版本:

import { module, test } from 'qunit';
import { visit, currentURL, click, find, findAll } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { Response } from 'miragejs';

module('Acceptance | foo', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  test('visiting /foo', async function (assert) {
    this.server.get('/foos', () => {
      return new Response(
        200,
        {},
        {
          foos: [
            {
              id: 20,
              name: 'foo-3'
            }
          ]
        },
      );
    })

    await visit('/foo');
    assert.equal(currentURL(), '/foo');
  });
});

You can read more about the MirageJS Response object on the official documentation and you can catch us answering this question live on the latest episode of May I Ask a Question您可以在官方文档上阅读更多关于 MirageJS Response object 的信息,您可以在最新一集的“我可以问一个问题”中看到我们现场回答这个问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM