-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Factory traits #852
Comments
I will give it a try this weekend if nobody has started working on traits yet, definitely a super useful feature. |
Would be amazing! Some folks had asked about a chaining API instead, but I can't think of one I like. |
@Azdaroth @samselikoff yes, I think #849 is better. You want to write real-life-like scenario for your seed-data, and this has to be flexible to encompass multiple cases / trees: so
What's wrong with that? |
Maybe I got too much used to using factory_girl, but still I would say traits are more natural to work with and the API is less complex, they work just as an extra extensions. At the end of the day, I think the difference between working with traits and chaining API would be |
@Azdaroth I would say nothing stops us from having traits for simple scenarios, and model hooks for bigger trees (not many hooks, only |
I spent some time trying to think of a chained API so we don't just cargo-cult factory_girl, but I couldn't think of one that doesn't introduce breaking changes. The problem with server.create('post').hasMany('comments') is that server.create('post')
.hasMany('comments')
.hasOne('author')
.value(); I then thought of how we could add // factories/post.js
export default Factory.extend({
title() {
return faker.lorem.sentence();
},
traits: {
published(post, server) {
post.update('isPublished', true);
},
withComments(post, server) {
server.createList('comment', 3, { post });
}
}
}); but alas JS has no types or piping, and this sucks: published(withComments(server.create('post'))) (not to mention those traits would need to be imported). Tried to think of a lot of other things but ultimately couldn't think of anything else without breaking API. We could add (now or eventually) another object that lets you chain: factory.create('post').published().withComments() but, yeah. Not backwards-compat. One last point, just adding the |
235945d closes |
Traits let you have different "versions" of a factory:
Invoke traits during
server.create
, by passing in a variable-length list of trait names:You can still pass in overrides, by passing in an object as the last argument:
The text was updated successfully, but these errors were encountered: