Consistent Factories in Mirage


#1

Hey everyone,

I’m using Mirage to mock data in my app and I was wondering if it’s possible to reference one field from another to make consistent mocks. For example, let’s say I have a BarnFactory:

BarnFactory = Mirage.Factory.extend({
  color() { return faker.random.arrayElement(['red', 'blue', 'green']) },
  name() { return faker.random.arrayElement(['Red Barn', 'Blue Barn', 'Green Barn']) }
});

this will give me a lot of variety in my mocks but it might look “wrong” in a demo to have a Barn object with the name “Blue Barn” and the color “red”.

Is it possible to do something like:

BarnFactory = Mirage.Factory.extend({
  color() { return faker.random.arrayElement(['red', 'blue', 'green']) },
  name() { return `${this.color().toUpperCase} Barn` }
})

So that the name is in sync with the color?

Thanks!


#3

Looks like this works with static attributes in v0.2 and is in the works for dynamic attributes: http://www.ember-cli-mirage.com/docs/v0.2.0-beta.3/factories/


#4

Thanks. I’ll have to check that out when it lands.