Sharing anonymous value fixture

Within a particular instance of AnonymousValueFixture, which is created for every builder, any generators that return a sequence of values (e.g. unique values) will be maintained. If you want to ensure that the same anonymous value fixture is used across multiple related builders then:

  • Using CreateListOfSize will automatically propagate the anonymous value fixture across builders
  • Call the GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null) method from within your custom builder, e.g.:
public MyCustomBuilder WithSomeValue(Func<SomeBuilder, SomeBuilder> modifier = null)
    return Set(x => x.SomeValue, GetChildBuilder<SomeObject, SomeBuilder>(modifier));
  • If using Builder<T> then call the SetUsingBuilder method, e.g.:
// Uses Builder<T>
    .SetUsingBuilder(x => x.Address)
// Uses Builder<T>, includes customisation
    .SetUsingBuilder(x => x.Address, b => b.Set(y => y.Street, "A street"))
// Uses AddressBuilder
    .SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address)
// Uses AddressBuilder, includes customisation
    .SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address, b => b.Set(y => y.Street, "A street"))
var student = Builder<StudentViewModel>.CreateNew()
    .Set(x => x.FirstName, any => any.FirstName())

There is currently no way to share an anonymous value fixture across unrelated builder instances. If this is something you need please raise an issue so we can discuss your requirement.

Did this page help you?