Skip to content

Instantly share code, notes, and snippets.

@jeradg
Last active March 3, 2016 19:47
Show Gist options
  • Save jeradg/6043ba31d831633ce4e3 to your computer and use it in GitHub Desktop.
Save jeradg/6043ba31d831633ce4e3 to your computer and use it in GitHub Desktop.
ember-cli-mirage Ember.Object factory - Use mirage factories to mock Ember Data models in component integration tests. See also ember-data-model-mock.js: https://gist.github.com/jeradg/6f8bfbbf0722d34eea09
/*
* Decorator on a Mirage plain-ol'-JavaScript-object factory
* to allow you to create Ember objects.
*
* The objects act just enough like an Ember Data model that
* you can use them as a drop-in replacement for DS.Model instances
* in many component integration tests.
*
* Returns an object with one method, `create`, that returns
* subclasses of Ember.Object that are created with the Mirage
* factory's faked data.
*
* (Works with ember-cli-mirage v2.x. Might also work with 1.x,
* but I haven't tested it.)
*/
export function mirageObjectFactory(MirageFactory) {
const factory = new MirageFactory();
const emberDataRecordAttrs = {
isValid: true,
isNew: false
};
return {
create(attrs) {
return Ember.Object.extend(emberDataRecordAttrs)
.extend(factory.build())
.create(attrs);
}
};
}
/*
// Example:
import SomethingFactory from 'frontend/mirage/factories/something';
export const Something = mirageObjectFactory(SomethingFactory);
// In your integration test
const something = Something.create({
name: 'I\'m just an Ember.Object, but I act sort of like a record!',
id: 101
});
this.set('model', something);
this.render(hbs`{{my-component model=model}}`);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment