AutoFixture setup interface property inside class
Asked Answered
I

1

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?

Isonomy answered 14/2, 2016 at 0:23 Comment(0)
R
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.

Rodl answered 14/2, 2016 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.