Mirage在接受测试中回复404

时间:2018-03-11 00:07:27

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

过去几天我一直在与海市蜃楼挣扎,但仍然无法找到解决方案。

即使使用非常简化的配置(见下文),只要在验收测试中调用,mirage就会响应404错误代码。 当我提供应用程序并查看浏览器控制台时,这些调用可以正常工作 (Mirage以200状态响应,数据在此处显示我设置的hello@world.com

这是我的mirage / config.js文件

// mirage/config.js

export default function() {
  this.get('/users', function() {
    return {
      data: [{
        type: 'user',
        id: 'first',
        attributes: {
          email: 'hello@world.com'
        }
      }]
    };
  });
}

这是我的app / routes / home.js

// app/routes/home.js
import Route from '@ember/routing/route';

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

这是失败的测试

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

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

  test('visiting /home', async function(assert) {
    await visit('/home');

    assert.equal(currentURL(), '/home');
  });
});

有了这条消息:

Promise rejected during "visiting /home": Ember Data Request GET /users 
returned a 404
Payload (Empty Content-Type)
Not found: /users@ 152 ms
Source:     
Error: Ember Data Request GET /users returned a 404
Payload (Empty Content-Type)
Not found: /users
    at ErrorClass.EmberError 
(http://localhost:7357/assets/vendor.js:24125:25)
    at ErrorClass.AdapterError 
(http://localhost:7357/assets/vendor.js:157167:15)
    at new ErrorClass (http://localhost:7357/assets/vendor.js:157185:22)
    at Class.handleResponse 
(http://localhost:7357/assets/vendor.js:169227:18)
    at ajaxError (http://localhost:7357/assets/vendor.js:169720:25)
    at Class.hash.error 
(http://localhost:7357/assets/vendor.js:169308:23)
    at fire (http://localhost:7357/assets/vendor.js:3607:31)
    at Object.fireWith [as rejectWith] 
(http://localhost:7357/assets/vendor.js:3737:7)
    at done (http://localhost:7357/assets/vendor.js:9646:14)
    at XMLHttpRequest.<anonymous> 
(http://localhost:7357/assets/vendor.js:9887:9)

谢谢

1 个答案:

答案 0 :(得分:2)

您确定Mirage在测试期间是否正在运行?

如果您使用的是最新版本的Ember,Mirage的默认初始化程序可能无法正常运行。 (这需要修复。)

您可能希望对the latest release notes进行阅读,并确保您使用的是版本0.4.2 +。

在新的测试方式中,您需要执行类似

的操作
  import { module, test } from 'qunit';
  import { visit, currentURL } from '@ember/test-helpers';
  import { setupApplicationTest } from 'ember-qunit';
+ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

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

    test('visiting /login', async function(assert) {
      await visit('/login');
      assert.equal(currentURL(), '/login');
    });
  });