Comments (3)
I think the reason the first example throws is that you're not returning the attrs
in the initializer.
The second example is the right approach. The following works:
const STEP_FREQ = 1
factory.define('packet', Object, {
time: factory.seq('Packet.time', i => Math.floor( Date.now() / 1000 ) + ( STEP_FREQ * i )),
}, {
afterBuild: model => {
model.time_to = model.time
return model
}
})
factory.build('packet').then(console.log) // => { time: 1507078473, time_to: 1507078473 }
from factory-girl.
I have plenty of test cases that dont require be to create or build or persist my models... in fact I don't have models at all. I just need to pump out sequential JSON objects with attribute duplicates. I tried the afterBuild approach but this doesn't get me what I need.
Why is this closed?
Is there a way to duplicate attributes so the values within a JSON object can be replicated?
from factory-girl.
For example ... I am trying to replicate the time
field in the time_to
field. For whatever irrelevant reason, the two have to have the same exact values.... I am trying to accomplish this by trying the buildOptions function which results in an unhandled promise rejection...
factory.define('packet', Object, buildOptions => {
let attrs = {
"curr_hour" : 0,
"device_id" : "ULP00037",
"last_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
"latitude" : factory.chance('latitude', { 'fixed': 13 }),
"longitude" : factory.chance('longitude', { 'fixed': 13 }),
"time": factory.seq( 'Packet.time',
(i) =>
{
let g = new Date();
return Math.floor( g.getTime()/1000 ) + ( STEP_FREQ * i );
}
),
"step_count": factory.seq( 'Packet.step_count',
(i) =>
{
return i * STEP_INTERVAL;
}
),
"packet_id" : factory.seq( 'Packet.packet_id',
( i ) =>
{
return i;
}
),
"prev_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
"proximity" : 0
};
attrs['time_to'] = attrs['time'];
});
let packet = factory.attrs('packet', {}, {})
...and the afterBuild
which results in a missing attribute inside the JSON...
factory.define('packet', Object, {
"curr_hour" : 0,
"device_id" : "ULP00037",
"last_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
"latitude" : factory.chance('latitude', { 'fixed': 13 }),
"longitude" : factory.chance('longitude', { 'fixed': 13 }),
"time": factory.seq( 'Packet.time',
(i) =>
{
let g = new Date();
return Math.floor( g.getTime()/1000 ) + ( STEP_FREQ * i );
}
),
"step_count": factory.seq( 'Packet.step_count',
(i) =>
{
return i * STEP_INTERVAL;
}
),
"packet_id" : factory.seq( 'Packet.packet_id',
( i ) =>
{
return i;
}
),
"prev_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
"proximity" : 0
}, {
afterBuild: model => {
model.time_to = model.time;
return model;
}
});
let packet = factory.attrs('packet')
You cover so many use cases in your docs... and issues. But this one?
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 HOT 1
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.