简体   繁体   English

在余烬气候0.2.x中重构模拟响应

[英]Refactoring mock response in ember cli mirage 0.2.x

I'm using ember cli mirage to write some acceptance tests for my Ember app. 我正在使用余烬程序为Ember应用编写一些验收测试。 I succeeded to mock server response for login but I'm not happy how I did it. 我成功模拟了用于登录的服务器响应,但是我不满意该怎么做。 Ember cli mirage have shorthands for route handlers and I would like to use them but everything I try throws me an error(except this solution). 灰烬的海市rage楼有路由处理程序的简写,我想使用它们,但是我尝试的所有事情都会抛出一个错误(此解决方案除外)。 Can someone help me to refactor this response? 有人可以帮助我重构此响应吗?

this.post('/login', ({ users, resources })=> {
  let user = users.first();

  if(!Ember.isEmpty(resources.first())){
    return {
      data: {
        type: 'user',
        id: user.id,
        attributes: user,
        relationships: {
          resources: {
            data: [
              { id: resources.first().id, type:  'resource' }
            ]
          }
        }
      },
    };
  } else {
    return {
      data: {
        type: 'user',
        id: user.id,
        attributes: user
      }
    };
  }
});

I have both user and resource model and factory defined, with relationships between them in user and resource model(it's many to many relationship). 我已经定义了用户模型和资源模型以及工厂,在用户模型和资源模型中它们之间具有关系(这是多对多关系)。 Here's how I create user in tests 这是我在测试中创建用户的方式

test('User can login', function(assert){
  let resources = server.createList('resource', 2),
      user      = server.create('user', {resources: resources});

  loginUser(user.email);
  andThen(()=>{
    assert.ok(find('a:contains("Logout")'));
    assert.equal('resource.content', currentPath());
  }); 
});

If it's many-to-many, you should explicitly create a join record, as direct m2m relationship support does not yet exist. 如果是多对多,则应显式创建一个联接记录,因为尚不存在直接的m2m关系支持。

// mirage/models/user.js
import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({

  userResources: hasMany()

});

// mirage/models/resource.js
import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({

  userResources: hasMany()

});

// mirage/models/user-resource.js
import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({

  user: belongsTo(),
  resource: belongsTo()

});

test('User can login', function(assert){
  let user = server.create('user');
  let resources = server.createList('resource', 2),

  // create the join records
  resources.forEach(resource => {
    server.create('user-resource', { user, resource });
  });

  loginUser(user.email);

  andThen(() => {
    assert.ok(find('a:contains("Logout")'));
    assert.equal('resource.content', currentPath());
  }); 
});

If you need to mock an endpoint that exposes the m2m directly it will take a bit more work. 如果您需要模拟直接暴露m2m的端点,则将需要更多的工作。 But in general I find that if your Ember app exposes CRUD operations on the relationship, it's good to expose the join record, too. 但是总的来说,我发现如果您的Ember应用程序公开关系中的CRUD操作,也最好公开联接记录。 Makes things simpler. 使事情变得简单。

That being said, Mirage will eventually support m2m relationships. 话虽如此,Mirage最终将支持m2m关系。

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

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