Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoFixture setup interface property inside class

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?

like image 886
ShaneKm Avatar asked Feb 14 '16 00:02

ShaneKm


Video Answer


1 Answers

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.

like image 149
dcastro Avatar answered Sep 28 '22 04:09

dcastro