GitHub地址:https://github.com/shouldly/shouldly

Shouldly的官方文档:http://docs.shouldly-lib.net/

Nuget安装:

一款简单易用的.Net 断言测试框架 : Shouldly-LMLPHP

在测试类中引用:

using Shouldly;

用法 :

//验证
UsingDbContext(context =>
{
var bill = context.Bills.FirstOrDefault();
bill.ShouldNotBeNull();
});

我们看一下,ShouldNotBeNull是如何定义的, 其实就是在在原类型的基础上加的扩展方法。

namespace Shouldly
{
[DebuggerStepThrough]
[ShouldlyMethods]
public static class ShouldBeNullExtensions
{
public static void ShouldBeNull<T>(this T actual);
public static void ShouldBeNull<T>(this T actual, string customMessage);
public static void ShouldBeNull<T>(this T actual, [InstantHandle] Func<string> customMessage);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual, string customMessage);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual, [InstantHandle] Func<string> customMessage);
}
}
[DebuggerStepThrough]
[ShouldlyMethods]
public static class ShouldBeTestExtensions
{
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this T actual, T expected);
public static void ShouldBe<T>(this T actual, T expected, string customMessage);
public static void ShouldBe<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder = false);
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance, string customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder, string customMessage);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance, string customMessage);
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance);
public static void ShouldBe(this double actual, double expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this double actual, double expected, double tolerance, string customMessage);
public static void ShouldBe(this double actual, double expected, double tolerance);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance, string customMessage);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance, string customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance);
public static void ShouldBe(this float actual, float expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this float actual, float expected, double tolerance, string customMessage);
public static void ShouldBe(this float actual, float expected, double tolerance);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected, string customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected);
public static T ShouldBeAssignableTo<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static T ShouldBeAssignableTo<T>(this object actual);
public static T ShouldBeAssignableTo<T>(this object actual, string customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThan<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeInRange<T>(this T actual, T from, T to, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeInRange<T>(this T actual, T from, T to, string customMessage) where T : IComparable<T>;
public static void ShouldBeInRange<T>(this T actual, T from, T to) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeLessThan<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeNegative(this decimal actual);
public static void ShouldBeNegative(this decimal actual, string customMessage);
public static void ShouldBeNegative(this decimal actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this double actual, string customMessage);
public static void ShouldBeNegative(this double actual);
public static void ShouldBeNegative(this double actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual);
public static void ShouldBeNegative(this long actual, string customMessage);
public static void ShouldBeNegative(this short actual, string customMessage);
public static void ShouldBeNegative(this short actual);
public static void ShouldBeNegative(this long actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this long actual);
public static void ShouldBeNegative(this int actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this int actual, string customMessage);
public static void ShouldBeNegative(this int actual);
public static void ShouldBeNegative(this short actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual, string customMessage);
public static T ShouldBeOfType<T>(this object actual);
public static T ShouldBeOfType<T>(this object actual, string customMessage);
public static void ShouldBeOfType(this object actual, Type expected, string customMessage);
public static void ShouldBeOfType(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static T ShouldBeOfType<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeOfType(this object actual, Type expected);
public static void ShouldBeOneOf<T>(this T actual, T[] expected, string customMessage);
public static void ShouldBeOneOf<T>(this T actual, params T[] expected);
public static void ShouldBeOneOf<T>(this T actual, T[] expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this float actual, string customMessage);
public static void ShouldBePositive(this decimal actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this short actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this short actual, string customMessage);
public static void ShouldBePositive(this short actual);
public static void ShouldBePositive(this decimal actual);
public static void ShouldBePositive(this decimal actual, string customMessage);
public static void ShouldBePositive(this float actual);
public static void ShouldBePositive(this double actual);
public static void ShouldBePositive(this double actual, string customMessage);
public static void ShouldBePositive(this double actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this float actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this long actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this long actual, string customMessage);
public static void ShouldBePositive(this long actual);
public static void ShouldBePositive(this int actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this int actual, string customMessage);
public static void ShouldBePositive(this int actual);
public static void ShouldBeSameAs(this object actual, object expected, string customMessage);
public static void ShouldBeSameAs(this object actual, object expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBeSameAs(this object actual, object expected);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected, string customMessage);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected);
public static void ShouldNotBeAssignableTo<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo<T>(this object actual, string customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected, string customMessage);
public static void ShouldNotBeAssignableTo<T>(this object actual);
public static void ShouldNotBeInRange<T>(this T actual, T from, T to) where T : IComparable<T>;
public static void ShouldNotBeInRange<T>(this T actual, T from, T to, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldNotBeInRange<T>(this T actual, T from, T to, string customMessage) where T : IComparable<T>;
public static void ShouldNotBeOfType(this object actual, Type expected, string customMessage);
public static void ShouldNotBeOfType<T>(this object actual, string customMessage);
public static void ShouldNotBeOfType(this object actual, Type expected);
public static void ShouldNotBeOfType(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOfType<T>(this object actual);
public static void ShouldNotBeOfType<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, T[] expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, T[] expected, string customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, params T[] expected);
public static void ShouldNotBeSameAs(this object actual, object expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeSameAs(this object actual, object expected, string customMessage);
public static void ShouldNotBeSameAs(this object actual, object expected);
}

当测试失败时,

一款简单易用的.Net 断言测试框架 : Shouldly-LMLPHP

异常测试:

                var bill = context.Bills.FirstOrDefault();
Should.Throw<DivideByZeroException>(() =>
{
bill.SetBmiNoPay();
});

测试结果:

一款简单易用的.Net 断言测试框架 : Shouldly-LMLPHP

04-15 02:45