Vadim's Weblog

Never stop learning.

Assert.Count in MbUnit version 3.2

Posted by Vadim on July 18, 2010

The Gallio team is about to release version 3.2 of Gallio and MbUnit.  If you’re impatient and like to eat your cake hot before it cools down, then download the Release Candidate.  The new version has many new great features.  Click here to see the list of enhancements and here for the bug fixes.

One of the new features is Assert.Count.  It’s impossible to count how many times I wrote code that verifies number of items in the specified collection:

        [Test]
        public void Test()
        {
            var list = new List<int> {2, 3, 5};
            Assert.AreEqual(3, list.Count);
        }

Now with new 3.2 version I can write my code like this:

        [Test]
        public void TestCount()
        {
            var list = new List<int> { 2, 3, 5 };
            Assert.Count(3, list);
        }

I assume that for most people it doesn’t look like a huge improvement and it’s probably not, but it definitely makes code more readable and defines clear the intentions of the specific assertion.

I don’t expect to surprise anybody by telling you that it also works with arrays.

        [Test]
        public void TestArray()
        {
            Assert.Count(3, new[] { 1, 2, 3 });
        }

Actually it works with any object that implements IEnumerable interface even if doesn’t have Count or Length property like CredentialCache class.

        [Test]
        public void TestEnumerable()
        {
            var myCache = new CredentialCache
                         {
                             {
                                 new Uri("http://www.contoso.com/"),
                                 "Basic",
                                 new NetworkCredential("userName", "password")
                             },
                             {
                                 new Uri("http://www.contoso.com/"),
                                 "Digest",
                                 new NetworkCredential("userName", "password", "domain")
                             }
                         };
            Assert.Count(2, myCache);
        }

It even works with string types (of course it does, string also implements IEnumerable).

        [Test]
        public void TestString()
        {
            Assert.Count(5, "Vadim");
        }

kick it on DotNetKicks.com

About these ads

One Response to “Assert.Count in MbUnit version 3.2”

  1. […] Also see this post from it’s author. […]

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: