AutoFixture 设置界面 属性 inside class

AutoFixture setup interface property inside class

如何让 AutoFixture 在包含接口 属性 的对象中填充属性?

public class Car : ICar
{
   public string Name { get; set; }
   public ICarinformation ContactInformation { get; set; } // problematic
            ...

public CarInformation: ICarinformation 
{
   public string Make {get; set; } // I would like these to be populated as well
   ...

fixture.Create() 抛出:

An exception of type 'Ploeh.AutoFixture.ObjectCreationException' occurred in Ploeh.AutoFixture.dll but was not handled in user code

Additional information: AutoFixture was unable to create an instance from Mediachase.Commerce.Inventory.ICarInformation because it's an interface

有没有办法为 属性 的 AutoFixture 提供具体类型?

是的,您可以只使用 TypeRelay 自定义

fixture.Customizations.Add(
    new TypeRelay(
        typeof(ICarInformation),
        typeof(CarInformation));

或者,如果您想通过 Moq 使用假对象,您可以使用 AutoMoqCustomizationAutoConfiguredMoqCustomization