Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 987
Aryan M Avatar asked Jul 15 '18 04:07

Aryan M


People also ask

Which attribute should be used to mark a method as test method in MSTest testing?

The TestClass attribute denotes a class that contains unit tests. The TestMethod attribute indicates a method is a test method.

What is MSTest used for?

MSTest is one type of framework that provides the facility to test the code without using any third-party tool. It helps in writing effective unit tests using MSTest framework to test software applications. MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE.

How do I run MSTest from command line?

MSTest utility. To access the MSTest tool, add the Visual Studio install directory to the path or open the Visual Studio Group from the Start menu, and then open the Tools section to access the Visual Studio command prompt. Use the command MSTest from the command prompt.


1 Answers

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);
    }
}
like image 148
Peska Avatar answered Sep 18 '22 16:09

Peska