【问题标题】:AutoFixture setup interface property inside class类内的 AutoFixture 设置接口属性
【发布时间】:2016-02-14 08:49:42
【问题描述】:

如何让 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() 抛出:

“Ploeh.AutoFixture.ObjectCreationException”类型的异常 发生在 Ploeh.AutoFixture.dll 中但未在用户代码中处理

附加信息:AutoFixture 无法创建实例 来自 Mediachase.Commerce.Inventory.ICarInformation 因为它是 界面

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

【问题讨论】:

    标签: c# .net autofixture


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多