If you absolutely must do it
If you look closer at the Inject method, you will notice that this is actually a general method, but the type argument is output when you use it, how you use it. If you want to freeze both, you can - you just need to call Inject for each type.
Customization slightly modified here. To prevent a possible invalid downgrade, I changed it so that its item field (and the corresponding argument to the item constructor) is of type Class :
public class Customization : ICustomization { readonly Class item; public Customization() : this(new Class()) { } public Customization(Class item) { this.item = item; } public void Customize(IFixture fixture) { fixture.Inject(item); fixture.Inject<IInterface>(item); } }
Note that Customize introduces the same item twice. In the first line, the generic type argument is output by the Class compiler, while in the second line, the IInterface type IInterface explicitly defined.
Given this implementation, the following test passes:
[Fact] public void UseCustomization() { var fixture = new Fixture().Customize(new Customization()); var c = fixture.Create<Class>(); var i = fixture.Create<IInterface>(); Assert.Equal(c, i); }
Using the built-in API
All that said, I would find it easier to just use the built-in API:
[Fact] public void UseTypeRelay() { var fixture = new Fixture(); fixture.Customizations.Add( new TypeRelay( typeof(IInterface), typeof(Class))); fixture.Freeze<Class>(); var c = fixture.Create<Class>(); var i = fixture.Create<IInterface>(); Assert.Equal(c, i); }
TypeRelay maps IInterface to Class , which means that all requests for IInterface will be passed to requests for Class . When Class frozen, it means that not only Class frozen, but there is IInterface .