Consider the following classes:
public interface IInterface {}
public class Class : IInterface {}
public class Customization : ICustomization
{
readonly IInterface item;
public Customization() : this( new Class() ) {}
public Customization( IInterface item )
{
this.item = item;
}
public void Customize( IFixture fixture )
{
fixture.Inject( item );
var created = fixture.Create<Class>(); // Would like this to resolve as item from previous line.
}
}
The problem I am running into is that the IInterface
is injected, whereas the Class
is not. Is there a way to inject both IInterface
and Class
so that the same instance is returned for both?
Please note that I would like to do this using an ICustomization
(or within an ICustomization
) and not with the attributes on a test-method. I am looking to do customized inject on these two classes. If I use [Frozen( Matching.ImplementedInterfaces)]Class item
as a parameter, it doesn't work, as the Class that is Frozen overwrites the injected value in the ICustomization.Customize
method.
Please additionally note that this is sample code and not how I am using it. In the xUnit Test Method, I would like the Class
instance that is specified as a parameter to be the frozen IInstance
above:
public void MyTest( IInterface @interface, Class implementation )
{
Assert.Same( @interface, implementation );
}
Freeze
overload doesn't do what you think it does; see the documentation. See Enrico Campidoglio's answer for one fairly idiomatic way of achieving the desired result. Another option would be to use one of the AutoFixture auto-mocking container extensions, which basically have such features built-in. – Potful