Vadim's Weblog

Never stop learning.

MbUnit 2.4 RTM: Now you can use Decimals in Row attributes.

Posted by Vadim on May 25, 2007

I switched from NUnit to MbUnit because of RowTest feature. You can read more about RowTest from Ben Hall’s post. I’ve been using RowTest(s) a lot. However, before MbUnit 2.4 RTM, it was impossible to use decimals in Row attribute. If you try to use code below with MbUnit version prior 2.4 RTM, you’ll get an ArgumentException with “Object of type ‘System.Double’ cannot be converted to type ‘System.Decimal’.” message.

[RowTest]
[Row(10.2, 2.5)]
public void DecimalTest(decimal x, decimal y)
{
  Assert.AreEqual(12.7, x + y);
}

The reason the exception happens is because for CLR, the decimal is not primitive type even if it’s for C#. Basicaly you cannot use decimal type with attributes.
MbUnit team was able to work around this limitation, so now you can safely use decimal in your Row attribute. However, there’re some cases that still not going to work even with the latest version. Here’s the example.

[RowTest]
[Row(21.5, 5.21, 4.1266794625719769673704414587)]
public void Div(decimal x, decimal y, decimal result)
{
  Assert.AreEqual(result, x / y);
}

If you try to run this test, you’ll see this message:

Equal assertion failed: [[4.12667946257198]]!=[[4.1266794625719769673704414587]]

Wait a second. Why do I see 4.12667946257198 when I explicitly typed 4.1266794625719769673704414587 for the third paramater in the Row attribute? The reason is the attributes don’t support decimals and the parameter was converted to double type.
MbUnit team haven’t find a solution for this limitation yet but they gave you a pretty good workaround. You can make this test work if you surround 4.1266794625719769673704414587 with double quotes.

This test should work with no problems at all.

[RowTest]
[Row(21.5, 5.21, "4.1266794625719769673704414587")]
public void Div(decimal x, decimal y, decimal result)
{
  Assert.AreEqual(result, x / y);
}

About these ads

4 Responses to “MbUnit 2.4 RTM: Now you can use Decimals in Row attributes.”

  1. Hi!

    Have you checked this? http://msdn2.microsoft.com/en-us/library/364x0z75(VS.80).aspx
    If you used the ‘m’ postfix, it should work as needed…

    Peter

  2. vkreynin said

    Peter,
    It’s CLR limitation, you cannot use decimal parameter in attributes. ‘m’ postfix will not solve the problem. You can try yourselves by putting 1.2m as a parameter for Row attribute. You’ll get the following compile error:
    “An attribute argument must be a constant expression, typeof expression or array creation expression”

  3. Great post Vadim :)
    I blogged about the other changes in MbUnit’s row based testing here http://hpoffice.cl/julian/archive/2007/06/01/MbUnits-RowTest-improvements-in-version-2.4.aspx

  4. Rowtest is defenitely a powerful attribute in Mbunit..

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: