c# - 单行多属性代码最小起订量

标签 c# moq

我有这些接口(interface)

public interface Interface1 { Interface2 Items {get;} }
public interface Interface2 { Guid? ApplicationTypeId { get; } }
public interface Interface3 { Class1 Item {get;} }
public interface Interface4 { Guid? ApplicationId { get; set; } }

一个类继承第一个接口(interface)

public class Class1 : Interface1 {
   public Interface2 Items { get; }
}

另一个由几个 guid 组成的类

public static class ContentTypeIds
{
    public static Guid ContentGuid1 => new Guid("{11798e9d-a167-4cfc-8cfa-9a24fd6caf25}");

    public static Guid ContentGuid2 => new Guid("{7d22f5bb-37fd-445a-b322-2fa1b108d260}");
}

我需要对以下属性进行单元测试

private readonly Interface3 _interface3;
public Ticket Current
{
   get
   {
      //This line is very complicated
      Interface4 itemByContentType = _interface3.Item?.Items.GetItemByContentType(ContentTypeIds.ContentGuid2);
      if ( itemByContentType?.ContentId != null )
          return Get(itemByContentType.ContentId.Value);
      return null;
   }
}

我的测试类在这里

[Test]
public class TestClass {
    var mock1 = new Mock<Interface1>();
    var mock2 = new Mock<Interface2>();
    var mock3 = new Mock<Interface3>();

    mock1.SetupAllProperties();
    mock2.SetupAllProperties();
    mock3.SetupAllProperties();
}

“itemByContentType”的值变为空。 任何人都可以帮助我让它变得简单和可测试,因为测试这个属性变得越来越复杂吗?我正在使用最小起订量。我将不胜感激任何帮助。

谢谢

最佳答案

我不是 Moq 方面的专家,但看起来它的 SetupAllProperties 方法只是将所有属性设置为像属性一样(即它创建的对象有一个持久成员,可以支持 GET/SET 操作)。如果不这样做,那么据我所知,这些属性仍然可用,但它们将始终解析为 null。这在准备 Mock 对象时非常方便,但就其本身而言,它不会为属性设置任何类型的值。

我认为您应该做的是结合使用 Moq 的 SetupGetReturns 方法来准备具有特定值的 Items 属性的 GET。

这是一些(简化的)示例代码,用于演示:

public interface IFoo { Guid? ApplicationId { get; set; } }
public interface IBar { IFoo Items { get; } }

class Program
{

    static void Main(string[] args)
    {
        // SETUP
        // Prepare mocks
        Mock<IFoo> MockFoo = new Mock<IFoo>();
        Mock<IBar> MockBar = new Mock<IBar>();

        // Seting up properties allows us read/write Foo's ApplicationId
        MockFoo.SetupAllProperties();

        // The mocked Foo object should be what's returned when Items is requested
        var expectedFoo = MockFoo.Object;

        // Setup the Bar object to return that mocked Foo
        MockBar.SetupGet(x => x.Items).Returns(expectedFoo);

        // The value written here will be persistent due to SetupAllProperties
        expectedFoo.ApplicationId = new Guid("{7d22f5bb-37fd-445a-b322-2fa1b108d260}");




        // ACTION
        // When the "Items" property is accessed, the IFoo we get should be what we mocked...
        var actualFoo = MockBar.Object.Items;

        // ... and we can read the value set to Foo's ApplicationId
        var actualAppId = actualFoo.ApplicationId;
    }
}

关于c# - 单行多属性代码最小起订量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43211344/

相关文章:

c# - 安装程序似乎没有注册并抛出 MockException

c# - 模拟打开对话框的 openDialog 框以从驱动器中选择文件名

c# - 起订接口(interface)

c# - 为什么在使用 Entity Framework 时看不到 Local 属性?

c# - ASP.NET MVC 身份数据库表映射问题

c# - 使用 Moq 部分模拟类内部方法

c# - 使用 Moq 实例引发 EventHandler<TEventArgs> 事件

c# - 从 CSV 文件中提取数据(融合表和 kml 解决方法)

c# - 可以使用外部软件播放/停止 youtube 视频吗?

c# - HTTPListener 不在 .NET 4.6 和 Server2012R2 上使用 TLS 1.1/1.2