Home > Blockchain >  Unit test for two string arrays
Unit test for two string arrays

Time:02-23

The below data row in my unit test throws an error message when two string arrays follow after one another, but not when I place another data type in-between.

[TestClass]
public class UnitTest
{
    [TestMethod]
    // invalid
    [DataRow(new string[] { }, new string[] { })]
    // valid
    [DataRow(new string[] { }, 8, new string[] { })]
    public void TestMethod(string[] input, string[] output)
    {
        var solution = new Program();

        CollectionAssert.AreEqual(output, solution.Method(input));
    }
}

And I get the following error (on line 6), an attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type. I'm defining the array in constructor, so how is it not constant? Thank you in advance.

CodePudding user response:

The second parameter in the DataRow attr is the params object[] moreData.

You are passing new string[] { } which differs from object[] this is why you are getting an error.

Try using this:

[DataRow(new string[] { }, new object[] { new string[] { } })]
public void TestMethod(string[] input, string[] output) {}

It would map objects array to strings correctly.

But you might consider using the DynamicData attribute to pass complex values.

  • Related