Reputation: 3414
AutoFixture does not recognize frozen object when filling dependencies.
public class Beta : IBeta
{
private string text;
public Beta(string text)
{
this.text = text;
}
}
public class Alpha : IAlpha
{
public IBeta beta;
public Alpha(IBeta beta)
{
this.beta = beta;
}
}
public void Test()
{
var fixture = new Fixture();
IBeta beta = fixture.Freeze<IBeta>(new Beta("test"));
IAlpha alpha = fixture.Create<Alpha>();
}
Yes, Alpha not IAlpha, because I want real Alpha with dependencies filled by AutoFixture.
PROBLEM: 'alpha.beta' is always CastleProxy, not my injected 'a' object...
Upvotes: 1
Views: 536
Reputation: 3414
I created an extension method. It solves my problem.
public static class AutoFixtureExtensions
{
public static TSeed FreezeSeed<TSeed>(this IFixture fixture, TSeed seed)
{
fixture.Inject<TSeed>(seed);
return seed;
}
}
Before:
IBeta beta = new Beta("test");
fixture.Inject<IBeta>(beta);
IAlpha alpha = fixture.Create<Alpha>();
beta.foo();
After v1:
// little gain in such use case
IBeta seed = new Beta("test");
IBeta beta = fixture.FreezeSeed<IBeta>(seed);
IAlpha alpha = fixture.Create<Alpha>();
beta.foo();
After v2:
// more self-describing
IBeta beta = fixture.FreezeSeed<IBeta>(new Beta("test"));
IAlpha alpha = fixture.Create<Alpha>();
beta.foo();
Actually, maybe not big difference, but it's more convenient and clear for me (than version with Inject()).
Upvotes: 0
Reputation: 233505
Use Inject
instead of Freeze
:
public void Test()
{
var fixture = new Fixture();
fixture.Inject<IBeta>(new Beta("test"));
IAlpha alpha = fixture.Create<Alpha>();
}
The Freeze
overload in the OP is for supplying a seed value, which, by default, is ignored by AutoFixture.
This is a known design error on my part - sorry about the confusion.
Upvotes: 2