ember-cli-mirage

Computed properties in Ember data models not available from ember-cli-mirage models


I try to use ember-cli-mirage for tests purposes. I have a lot of Ember data models with computed properties. When i'm creating a Mirage model instance, it seems that computed properties are not available. I was wondering if i'm making something wrong of if there's a way to get computed properties works ?

// app/models/profile.js
import DS from 'ember-data';

export default DS.Model({
  firstName: attr('string'),
  lastName: attr('string'),

  fullName: computed('firstName', 'lastName', function() {
    // return ...
  })
});
// Create profile instance...
let profile = server.create('profile', { firstName: 'Tom', lastName: 'Stran' });
profile.firstName // Tom
profile.lastName  // Stran
profile.fullName // undefined
profile.get('fullName') // profile.get is not a function

Thanks you !


Solution

  • You're not making a mistake - Mirage models do not inherit your Ember Data model's computed properties. I know it's confusing, and you're certainly not the first to have asked why.

    Mirage models are called "models" because in most systems (front-end and back-end), the term Model means "an object wrapping some data." Even though Mirage is designed to work with Ember, Mirage actually has no knowledge of your Ember app at all.

    Mirage has its own "database" (a JavaScript object), and ORM. Models are a convenience for you to "seed your server" with data suitable for you to develop and test your Ember application. So, in the same way that your Ruby models would not have access to your Ember Data computed properties (if, say, your server was written in Rails), your Mirage models also exist in an isolated environment.

    It recent versions, Mirage is able to look at your Ember Data models and copy their relationships for its own schema. But this is just a convenience, and is only used for Mirage's bootstrapping process.

    Let me know if that clears things up, or if you have any more questions!