Comments (8)
@jonatanklosko, sorry for a long delay!
I have raised #73 as per @aexmachina 's suggestion. The afterBuild
callback should now be invoked on create
calls as well (once the PR gets merged :)).
from factory-girl.
I've just realised that it's not invoked after #attrs
as well.
from factory-girl.
Hi @jonatanklosko, the order of execution for these methods is attrs
-> build
-> create
. As of now, we just have callbacks for afterBuild
and afterCreate
, hence the callbacks are not executed for attrs
call. I'll open another issue to have an afterAttrs
callback.
from factory-girl.
Hi @jonatanklosko,
You can try following to achieve the same effect:
Factory.define('user', User, {
username: Factory.sequence(n => `sherlock_${n}`),
name: 'Sherlock Holmes',
password: 'password',
passwordConfirmation: null,
}, {
afterBuild: model => {
model.passwordConfirmation = model.password;
return model;
},
});
Just to add some details, the problem with having it the way you suggested is how to ensure that passwordConfirmation
is evaluated after password
.
You can use the afterBuild
callback to modify the built object anyway you want!
Let us know in case you face any issues.
from factory-girl.
Hey @chetanism,
I've already tried this and actually it should be
afterBuild: model => {
model.passwordConfirmation = model.passwordConfirmation || model.password;
return model;
},
The problem with this solution is that afterBuild
is not invoked when you use create
.
I can think of a feature, that would allow us to do:
/* ... */
password: "secret",
passwordConfirmation: factory.duplicate('password')
/* ... */
/* Or even more flexible */
/* ... */
password: "secret",
passwordConfirmation: factory.after('password', password => /* do something and return */)
/* ... */
This way you can evaluate all normal attributes and those duplicate
/after
calls at the end.
What are your thoughts =) ?
from factory-girl.
@chetanism I think maybe we should consider calling both afterBuild
and afterCreate
when creating. I think this makes sense because create
does follow build
.
from factory-girl.
Yeah this behavior makes more sense. The PR looks great, thanks! =)
from factory-girl.
#73 has been merged, closing this issue now. Please advise if there's any issues.
from factory-girl.
Related Issues (20)
- Expose passed attrs to initializer function HOT 1
- concatonating values from chance generator
- Add to CHANGELOG that `buildSync` is gone in v4
- factory.assoc is not taking attributes values from factory.create
- Resetting Sequence HOT 1
- UnhandledPromiseRejectionWarning: TypeError: Model is not a constructor
- ENHANCEMENT: Async buildOptions on extended factory not taking the base factory values HOT 1
- afterCreate() in Sub-Object Creates Weird Error HOT 1
- make compatible mongoose 5.29 populate
- TypeScript definition HOT 5
- Calling create "hangs" during create
- Is this project still maintained? HOT 7
- Undefined attribute on initializer cause error HOT 1
- How to define @many to many ? HOT 1
- delete objects after test suite HOT 3
- Is it possible to use build options with an extended factory? HOT 1
- Invalid params when callback afterBuild and afterCreate in case create/buildMany
- How to use a factory definition in a separate file? HOT 1
- When using factory with jest and mongoose, process doesn't end HOT 1
- [DEP0128] DeprecationWarning: Invalid 'main' field
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory-girl.