This is not really a question, since I can make it work. It's just something I have not seen before and I think it is odd. I'm writing some unit tests. One test works fine, but when I change a parameter for another test, it gives an error. The parameters do match what is passed into the methods. I just didn't retype the test method attribute each time here. This is not my actual code because that is the property of my employer and I am not allowed to post any of it. Hopefully there are no typos due to typing it out instead of copy/paste. This is C#.

Have any of you seen this before, and do you know why it would do this?

[TestMethod]
[DataRow(enumName.SomeValue, "stringValue", new string[] {"aryVal1","aryVal2"})] //this works, but when I remove the string value, it does not

[DataRow(enumName.SomeValue, new string[] {"aryVal1","aryVal2"})] //this gives error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

[DataRow( new string[] {"aryVal1","aryVal2"}, enumName.SomeValue)] //this works, but all I did was change the order of the parameters

The error you are seeing is because of the way DataRowAttribute is defined. It can handle arrays of simple types. The issue here is that DataRowAttribute has the overload for more than one parameters: DataRowAttribute(object data1, params object[] moreData). In your expression, C# takes the second string array as the object[] moreData and it doesn’t like it. You are correct to try and change the order in your 3rd example. You can also try the helper method, which will return an object array with all the values you want to pass to DataRowAttribute.

public enum enumName
{
    SomeValue
}

[TestClass]
public class TestClass
{
    [TestMethod]
    [DataRow(enumName.SomeValue, new string[] { "aryVal1", "aryVal2" })]
    public void TestMethod(enumName enumValue, string[] stringArray)
    {
        // test code
    }

    private object[] GetDataRowValues(enumName enumValue, string[] stringArray)
    {
        return new object[] { enumValue, stringArray };
    }
}

helper method:

[DataRow(dataRowValues: new object[] { 1, new string[] { "aryVal1", "aryVal2" } })]

Yes, issues like the one you're describing can occur in unit testing for several reasons, and they are often subtle and tricky to diagnose. Here are some common causes and things to check

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.