user18280464
user18280464

Reputation:

Unit test for two string arrays

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.

Upvotes: 0

Views: 603

Answers (2)

josselin thevenet
josselin thevenet

Reputation: 1

What is crazy is that the two following methods works :

[DataRow(new string[] { "Item1" }, "1", new string[] { "Item1" })]
public void MyTest(string[] argArray, string expectedResult, string[] expectedResultArray)
{
    // ... 
}

and

[DataRow("1", new string[] { "Item1" }, new string[] { "Item1" })]
public void MyTest(string arg1, string[] argArray, string[] expectedResultArray)
{
    // ... 
}

Edit : best answer is here (second one) : Using List<string> type as DataRow Parameter

Upvotes: 0

NazaRN
NazaRN

Reputation: 419

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.

Upvotes: 1

Related Questions