Skip to content
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

How to randomize buildList results #34

Open
ahahn95 opened this issue Feb 11, 2020 · 5 comments
Open

How to randomize buildList results #34

ahahn95 opened this issue Feb 11, 2020 · 5 comments

Comments

@ahahn95
Copy link

ahahn95 commented Feb 11, 2020

BuildList always returns the same result, even though I'm using faker to try to auto generate values. Is there a way this can be randomized?

Example:

const results = myFactory.buildList(3);

Expected

{
[dayOftheWeek: 'Tuesday'],
[dayOftheWeek: 'Wednesday'],
[dayOftheWeek: 'Friday'],
}

Actual

{
[dayOftheWeek: 'Tuesday'],
[dayOftheWeek: 'Tuesday'],
[dayOftheWeek: 'Tuesday'],
}
@willryan
Copy link
Owner

can you show me how myFactory is defined? I ran a quick test and buildList generates a new value each time for me.

Without seeing your code for the factory, my first inclination would be to make sure you are using Sync.each(n => generateValue()) and not just generateValue(), and if you are then to figure out if you want/need to use the sequence number to affect the output value.

@ahahn95
Copy link
Author

ahahn95 commented Feb 12, 2020

@willryan My factory looks something like this (doesn't match the above example, but you get the idea)

const testFactory = Factory.makeFactory<myInterface>({
randNumber: faker.random.number()
})

const results = testFactory.buildList(5);

@willryan
Copy link
Owner

willryan commented Feb 12, 2020

ok, yeah, that needs to look like:

const testFactory = Factory.makeFactory<myInterface>({
  randNumber: Sync.each(() => faker.random.number())
})

const results = testFactory.buildList(5);

Without the each() call (imported from the same place as Factory), the value will not be generated on each invocation of build(). each() returns a special Generator object that factory.ts knows means it has to run on each invocation. If a factory is constructed with a value that is not a generator, it just returns that value each time (unless overridden by build()).

@willryan
Copy link
Owner

on a semi-related note, the callback argument for Sync.each() provides a sequence number which you can use to adjust the output if you want. This can allow for seemingly-random-but-predictable sequences, if need be.

@dayinji
Copy link

dayinji commented May 25, 2020

on a semi-related note, the callback argument for Sync.each() provides a sequence number which you can use to adjust the output if you want. This can allow for seemingly-random-but-predictable sequences, if need be.

I encounter this question, and when I use each() function, a new question was triggered by typescript, which said 'Type 'Generator' is not assignable to type 'number'.'.

How can I deal with it ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants