1

I have a fixture create which looks somewhat like this.

// mirage/fixtures/people.js
 export default {
      'people': [
        {
          'id': 1,
          'name': 'Ram',
         },
         {
          'id': 2,
          'name': 'Raja',
         }
       ]
    }

Inside my acceptance test I'm using this array. But within my test, I want to modify this people array and add, suppose another object

{
   'id': 3,
   'name': 'John',
}

Note: I dont want to use factories as I dont want all datas to be dynamically generated, so I want to take this array from fixtures, push my new object into this array and then return it. What is the correct way to do it?

Note2: Don't suggest adding this object in fixtures itself, because I want to dynamically add items to the fixture based on conditions in my test.

Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
siwalikm
  • 1,792
  • 2
  • 16
  • 20
  • 1
    I am a bit confused. Which Mirage version are you using? AFAIK `server.create` is used only with factories. In order to load fixture data you should run [`server.loadFixtures()`](http://www.ember-cli-mirage.com/docs/v0.3.x/configuration/#loadFixtures) – Ramy Ben Aroya May 23 '17 at 23:48
  • Hi @RamyBenAroya, in my config.js for mirage, I'm doing `import peopleFromFixture from '/mirage/fixtures/people';` `this.get('/people', (schema, request) => {` ` return peopleFromFixture;` ` });` – siwalikm May 24 '17 at 08:54
  • This sounds wrong. You never create the models in the mirage db. You basically created your own small db for people which is the exported object in `/mirage/fixtures/people`. – Ramy Ben Aroya May 24 '17 at 10:10
  • @RamyBenAroya sorry Indeed I was not using server.create for fixture. My bad, i've edited the question now but my original question still remains. – siwalikm May 24 '17 at 10:36

1 Answers1

1

This was pretty straight forward. In the mirage config, we shouldn't be doing this

// import peopleFromFixture from '/mirage/fixtures/people'; 
// this.get('/people', (schema, request) => {  
// return peopleFromFixture;  }); 

instead read data from factories and populate original fixture values with server.loadFixtures('people').

So config.js will look like =>

this.get('/people'); 

Set your factory like this =>

import { Factory } from 'ember-cli-mirage';
export default Factory.extend({
  id(i) {  return i+1; },
  name() { return faker.name.findName(); }
});

Inside your test case, populate original and new values like this =>

server.loadFixtures('people');
server.create('people', { name: 'John' });
siwalikm
  • 1,792
  • 2
  • 16
  • 20