Assuming you have a set of derived types, for example:
public class A: KeyedEntity<A> { } public class B: KeyedEntity<B> { }
Since the above graph of objects contains a circular link (to T ), you need to configure the Fixture instance to exclude assignments during the first recursion:
Then you Create a generic method that will set up the creation algorithm for KeyedEntity<T> :
internal void CustomizeKeyedEntity<T>(IFixture fixture) { fixture.Customize<KeyedEntity<T>>(c => c.FromFactory(() => KeyedEntity.Create( fixture.Create<Identifier>(), fixture.Create<T>()))); }
You can use the above method like:
this.CustomizeKeyedEntity<A>(fixture); this.CustomizeKeyedEntity<B>(fixture);
Example
[Fact] public void Test() { var fixture = new Fixture(); this.CustomizeKeyedEntity<A>(fixture); this.CustomizeKeyedEntity<B>(fixture); var actualA = fixture.Create<A>(); var actualB = fixture.Create<B>(); }
Nikos Baxevanis
source share