.Net, MbUnit

MbUnit: Arrays with RowTest

At my place of work some people were surprised that it’s possible to put array of values in Row attribute. I hope that some people can find this information useful.

[Row(new string[] { "0", "4"}, new int[] {0, 4})]
public void CompareArrays(string[] strArr, int[] intArr)
   int len = strArray.Length;
   Assert.AreEqual(len, intArr.Length);
   for (int i = 0; i < len; i++)
      Assert.AreEqual(strArr[i], intArr[i].ToString());

I often use arrays to create an IEnumerable type like List<T>.

[Row(new string[] { "str1", "str2" }, 2)]
public void ListTest(string[] strArray, int length)
   List<string> strList = new List<string>(strArray);
   Assert.AreEqual(length, strList.Count);

There’s one known problem with using arrays. You’ll get a compile error like this one “An attribute argument must be a constant expression, typeof expression or array creation expression” if an array is only parameter in Row attribute. Code below will not compile.

[Row(new string[] { "str1", "str2" })]
public void NoComplie(string[] strArray)

I use this feature of MbUnit a lot. 

Empower yourself with MbUnit.


2 thoughts on “MbUnit: Arrays with RowTest

  1. For fixing problem with array only param I can propose to introduce fake parameter:

    [Row(new string[] { “str1″, “str2″ }, new object())]
    public void Complie(string[] strArray, object fake)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s