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.

[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.

[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.

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


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

  1. Hi!

    Have you checked this?
    If you used the ‘m’ postfix, it should work as needed…


  2. vkreynin said

    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

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

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: