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

Referencing other factories in one kotlinFixture block. #102

Closed
milgner opened this issue Dec 5, 2023 · 1 comment
Closed

Referencing other factories in one kotlinFixture block. #102

milgner opened this issue Dec 5, 2023 · 1 comment

Comments

@milgner
Copy link

milgner commented Dec 5, 2023

When building complex data structures, I'd like to invoke other factories from within the current one.

Something along the lines of

val myFixtures = kotlinFixture {
    factory<MyComplexObject> { buildAComplexObject() }
    factory<AnotherObject> { AnotherObject(complexStuff = buildFromPreviousFactory<MyComplexObject>()) }
}

Unfortunately I haven't found a way to do this with the current API except through multiple kotlinFixture blocks. Any ideas?

@mattmook
Copy link
Member

Hi @milgner, inside the generator block of the factory you have access to fixture which will use any factories already defined. So basically for your example you can simply write:

val myFixtures = kotlinFixture {
    factory<MyComplexObject> { buildAComplexObject() }
    factory<AnotherObject> { AnotherObject(complexStuff = fixture<MyComplexObject>()) }
}

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

2 participants