Using AutoFixture 3.50 and xUnit.NET, it seems that there is a difference between the way that Fixture.Create() creates concrete objects and the way that AutoData Theory tests create concrete objects.
Simple example:
public class Foo
{
private string prop;
public string Prop
{
get
{
if (prop == null) { prop = "Prop"; } // Breakpoint 'A'
return prop;
}
}
}
Test using Fixture:
[Fact]
public void FixtureTest()
{
var fixture = new Fixture();
var result = fixture.Create<Foo>(); // Breakpoint 'B1'
}
Test using AutoDataAttribute:
[Theory, AutoData]
public void AutoDataTest(Foo sut)
{
var bar = 1; // Essential no-op, Breakpoint 'B2'
}
In the former test, breakpoint 'B1' is hit while Breakpoint 'A' is never hit. In the latter test, breakpoint 'A' is hit before breakpoint 'B2' is hit. This is problematic when I have "lazily" initialized property not dissimilar from the one above - because the backing field of the property is initialized before the test is run, I cannot test the initialization logic.
Is there a way to customize AutoDataAttribute so that I can get around this behavior? Or possibly, maybe this is a bug?