More fluent assertions using extension methods
Posted by Garry Shutler in Extension methods, MbUnit, Unit testing
I'm a great believer in making your unit tests as easy to read and understand as possible. I was thinking about how I could improve my tests the other day and thought about using extension methods to create a more fluent way of creating complex assertions.
This has allowed me to create a complex assertion like:
- testObject.ShouldBeTheSameObjectAs(targetObject).And.ShouldBeEqualTo(testObject).And.ShouldSatisfy(x => x is Object);
Which I hope you'll agree reads a lot easier than those three assumptions on separate lines. Also, note that I have added the ability to use a predicate for when there isn't a method which quite matches your needs.
Here's the code for the extension methods:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MbUnit.Framework;
- namespace UnitTestingExtensions
- {
- public static class FluentTestingExtensions
- {
- public static FluentAnd<T> ShouldSatisfy<T>(this T testTarget, Predicate<T> predicate)
- {
- Assert.IsTrue(predicate.Invoke(testTarget));
- return new FluentAnd<T>(testTarget);
- }
- public static FluentAnd<T> ShouldBeEqualTo<T>(this T testTarget, T comparisonObject)
- {
- Assert.AreEqual(testTarget, comparisonObject);
- return new FluentAnd<T>(testTarget);
- }
- public static FluentAnd<T> ShouldBeTheSameObjectAs<T>(this T testTarget, Object comparisonObject)
- {
- Assert.AreEqual(testTarget, comparisonObject);
- return new FluentAnd<T>(testTarget);
- }
- public class FluentAnd<T>
- {
- public T And { get; private set; }
- public FluentAnd(T target)
- {
- this.And = target;
- }
- }
- }
- }
And here are the tests for the extension methods:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MbUnit.Framework;
- namespace UnitTestingExtensions.FluentTestingExtensionsTests
- {
- [TestFixture]
- public class ShouldSatisfyTests
- {
- [Test, ExpectedException(typeof(MbUnit.Core.Exceptions.AssertionException))]
- public void PredicateEvaluatesAsFalseAndTestFails()
- {
- int testNumber = 4;
- testNumber.ShouldSatisfy(x => x > 10);
- }
- [Test]
- public void PredicateEvaluatesAsTrueAndTestPasses()
- {
- int testNumber = 4;
- testNumber.ShouldSatisfy(x => x == 4);
- }
- }
- [TestFixture]
- public class ShouldBeEqualToTests
- {
- [Test, ExpectedException(typeof(MbUnit.Core.Exceptions.AssertionException))]
- public void EqualsCheckFailsAndTestFails()
- {
- int testNumber = 4;
- testNumber.ShouldBeEqualTo(7);
- }
- [Test]
- public void EqualsCheckPassesAndTestPasses()
- {
- int testNumber = 4;
- testNumber.ShouldBeEqualTo(4);
- }
- }
- [TestFixture]
- public class ShouldBeTheSameObjectAsTests
- {
- [Test, ExpectedException(typeof(MbUnit.Core.Exceptions.AssertionException))]
- public void DifferentObjectsSoTestFails()
- {
- Object testObject = new Object();
- testObject.ShouldBeTheSameObjectAs(new Object());
- }
- [Test]
- public void TestPassesWhenTheObjectsAreTheSame()
- {
- Object testObject = new Object();
- Object checkObject = testObject;
- testObject.ShouldBeTheSameObjectAs(checkObject);
- }
- }
- [TestFixture]
- public class AndTests
- {
- [Test, ExpectedException(typeof(MbUnit.Core.Exceptions.AssertionException))]
- public void TestFailsIfFirstCaseFails()
- {
- int testNumber = 4;
- testNumber.ShouldBeEqualTo(3).And.ShouldSatisfy(x => x > 1);
- }
- [Test, ExpectedException(typeof(MbUnit.Core.Exceptions.AssertionException))]
- public void TestFailsIfSecondCaseFails()
- {
- int testNumber = 4;
- testNumber.ShouldBeEqualTo(4).And.ShouldSatisfy(x => x > 10);
- }
- [Test]
- public void TestPassesIfBothCasesPass()
- {
- int testNumber = 4;
- testNumber.ShouldSatisfy(x => x > 1).And.ShouldBeEqualTo(4);
- }
- [Test]
- public void TestPassesWhenThreeCasesAreUsed()
- {
- Object testObject = new Object();
- Object targetObject = testObject;
- testObject.ShouldBeTheSameObjectAs(targetObject).And.ShouldBeEqualTo(testObject).And.ShouldSatisfy(x => x is Object);
- }
- }
- }
I'd love to hear some feedback on what you think of this style.
This entry was posted on at Tuesday, January 29, 2008 and is filed under Extension methods, MbUnit, Unit testing. You can follow any responses to this entry through the RSS 2.0. You can leave a response.
