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?
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);
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With