7

How can I have AutoFixture populate properties in my object containing interface property?

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() throws:

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

Is there a way to provide AutoFixture with Concrete type for that property?

ShaneKm
  • 20,823
  • 43
  • 167
  • 296

1 Answers1

9

Yes, you can just use the TypeRelay customization

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

Alternatively, if you wanted to use a fake object using Moq, you could use either AutoMoqCustomization or AutoConfiguredMoqCustomization.

dcastro
  • 66,540
  • 21
  • 145
  • 155