单元测试:使用MSTest

时间:2018-07-15 04:50:48

标签: c# unit-testing 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)
}

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

1 个答案:

答案 0 :(得分:4)

使用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);
    }
}