Created
September 13, 2016 12:12
-
-
Save iHaveBecomeDeath/fb3992da9194084fcd6ab4644403c037 to your computer and use it in GitHub Desktop.
NUnit assertion extensions, write test code from left to right
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some simple extensions I lug around to semantically simplify my test writing | |
// instead of saying "assert that this and that are equal, using some formula" | |
// you can say (1 == 1).ShouldBeTrue(), or 1.ShouldEqual(1); | |
// Some methods return the object in question, so you can chain assertions. | |
// Using System | |
// Using NUnit.Framework | |
public static class AssertionExtensions | |
{ | |
public static object ShouldEqual(this object actual, object expected) | |
{ | |
Assert.That(actual.Equals(expected)); | |
return actual; | |
} | |
public static T ShouldEqual<T>(this object actual, object expected) | |
{ | |
actual.ShouldEqual(expected); | |
return (T)actual; | |
} | |
public static object ShouldNotEqual(this object actual, object expected) | |
{ | |
Assert.That(actual.Equals(expected), Is.False); | |
return actual; | |
} | |
public static T ShouldNotEqual<T>(this object actual, object expected) | |
{ | |
actual.ShouldNotEqual(expected); | |
return (T)actual; | |
} | |
public static void ShouldBeTrue(this object actual) | |
{ | |
Assert.That(actual, Is.True); | |
} | |
public static void ShouldBeFalse(this object actual) | |
{ | |
Assert.That(actual, Is.False); | |
} | |
public static object ShouldNotBeNull(this object hopefullyNotNull) | |
{ | |
Assert.That(hopefullyNotNull, Is.Not.Null); | |
return hopefullyNotNull; | |
} | |
public static object ShouldBeNull(this object shouldReallyBeNull) | |
{ | |
Assert.That(shouldReallyBeNull, Is.Null); | |
return shouldReallyBeNull; | |
} | |
public static T ShouldBeInstanceOfType<T>(this object objectOfAssertion) | |
{ | |
Assert.IsInstanceOf<T>(objectOfAssertion); | |
return (T)objectOfAssertion; | |
} | |
public static void ShouldContain(this string metaphoricalHaystack, string needle) | |
{ | |
Assert.That(metaphoricalHaystack.Contains(needle)); | |
} | |
public static void ShouldContain(this ICollection metaphoricalHaystack, object needle) | |
{ | |
Assert.Contains(needle, metaphoricalHaystack); | |
} | |
public static void ShouldBeEmpty(this IEnumerable collection) | |
{ | |
Assert.IsEmpty(collection); | |
} | |
public static void ShouldNotBeEmpty(this IEnumerable collection) | |
{ | |
Assert.IsNotEmpty(collection); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment