简体   繁体   中英

Unit testing: Sending complex object as a input parameter to my test method using MSTest

I'm trying to send a List<DataStatusItem> as a input parameter to my unit test method using DataRow attribute as below,

[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)
}

Please let me know how to send the List as a input parameter to my test method.

Use DynamicData Attribute , Here is an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM