其他分享
首页 > 其他分享> > 单元测试:使用MSTest将复杂对象作为输入参数发送到我的测试方法

单元测试:使用MSTest将复杂对象作为输入参数发送到我的测试方法

作者:互联网

我正在尝试发送List< DataStatusItem>作为使用DataRow属性的单元测试方法的输入参数,如下所示,

[TestClass]
public class UpdateProcessingTestController
{
    private List<DataStatusItem> DataStatusItemsTestSetup = new List<DataStatusItem>() {
            new DataStatusItem { DataItemID = 1, DataItemCurrentStatusID = 1, DataItemStatusID = 1, DateEffective = DateTime.Now }
    };

    private readonly Mock<IEmployee> moqEmployee;

    public UpdateProcessingTestController()
    {
        moqEmployee = new Mock<IEmployee>();
    }

    [TestMethod]
    [DataRow(DataStatusItemsTestSetup, 1, 8, 1)] // **This is where it is throwing me compilation error**
    public void SetDataItems(List<DataStatusItem> DataStatusItems,int brand, int dataType, int processingStatus)
}

请让我知道如何将列表作为输入参数发送到我的测试方法.

解决方法:

使用DynamicData Attribute,这是一个示例:

public class DataStatusItem
{
    public int DataItemID { get; set; }
    public int DataItemCurrentStatusID { get; set; }
    public int DataItemStatusID { get; set; }
    public DateTime DateEffective { get; set; }
}

[TestClass]
public class UpdateProcessingTestController
{
    static IEnumerable<object[]> DataStatusItemsTestSetup
    {
        get
        {
            return new[]
            {
                new object[]
                {
                    new List<DataStatusItem>
                    {
                        new DataStatusItem { DataItemID = 1, DataItemCurrentStatusID = 1, DataItemStatusID = 1, DateEffective = DateTime.Now },
                        new DataStatusItem { DataItemID = 2, DataItemCurrentStatusID = 2, DataItemStatusID = 2, DateEffective = DateTime.Now },
                    },
                    1, // brand
                    2, // dataType
                    3  // processingStatus
                }
            };
        }
    }

    [TestMethod]
    [DynamicData(nameof(DataStatusItemsTestSetup))]
    public void SetDataItems(List<DataStatusItem> dataStatusItems, int brand, int dataType, int processingStatus)
    {
        Assert.AreEqual(2, dataStatusItems.Count);
        Assert.AreEqual(1, brand);
        Assert.AreEqual(2, dataType);
        Assert.AreEqual(3, processingStatus);
    }
}

标签:unit-testing,mstest,c
来源: https://codeday.me/bug/20191025/1925779.html