Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did you extend your Assert class [closed]

I love to Extend my Assert.AreEqual to many different classes, the known one is the CollectionAssert of course, but I can think of some more such as: ImageAssert, XmlAssert etc..

Did you Create your own Assert classes? and what kind of new would you like to create?

like image 251
rabashani Avatar asked Dec 19 '08 16:12

rabashani


2 Answers

That's my solution:

using MyStuff;

using A = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace Mytestproj.Tests
{
    public static class Assert
    {
        public static void AreEqual(object expected, object actual)
        {
            A.AreEqual(expected, actual);
        }

        // my extension
        public static void AreEqual(MyEnum expected, int actual)
        {
            A.AreEqual((int)expected, actual);
        }

        public static void IsTrue(bool o)
        {
            A.IsTrue(o);
        }

        public static void IsFalse(bool o)
        {
            A.IsFalse(o);
        }

        public static void AreNotEqual(object notExpected, object actual)
        {
            A.AreNotEqual(notExpected, actual);
        }

        public static void IsNotNull(object o)
        {
            A.IsNotNull(o);
        }

        public static void IsNull(object o)
        {
            A.IsNull(o);
        }
    }
}
like image 89
Omu Avatar answered Nov 20 '22 17:11

Omu


I like the feel of the Assert class, but wanted something that would serve more as a general validation framework. I started with Roger Alsing's article on using extension methods, and now have a system that works like:

Enforce.That(variable).IsNotNull();
Enforce.That(variable).IsInRange(10, 20);
Enforce.That(variable).IsTypeOf(typeof(System.String));
etc.

If any enforcement fails, it throws an exception. I have been considering refactoring so that I can also incorporate a non-critical evaluation that does not throw an exception. Some like Check.That as a variant of Enforce.That which would return booleans, but have extension methods with identical signatures.

What I like about this approach, so far, is that I can use these in my unit tests, as well as for pre-validation and post-validation concerns in my actual code without referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. I put it in my root assembly for my application framework, and Enforce is at the root, so it is very easy to get to.

like image 4
Joseph Ferris Avatar answered Nov 20 '22 16:11

Joseph Ferris