Project DescriptionA new way of performing lambda-based assertions on tests so that they remain agnostic to the underlying test framework, and leverage .NET built-in expressions syntax.
This project provides a single source file subset of
Fabio Maulo's
#TestEx, for performing lambda-based assertions on unit tests.
In order to verify state of objects and returned values from your object under test, you simply use your .NET language to build the lambda expression to verify. Let's see some examples which make it much easier to understand why this is helpful.
Suppose you want to ensure a returned string value is exactly equal to another:
var result = "Hello world";
result.Satisfies(s => s == "Hello World");
Similar to what your unit testing framework of choice would typically do, this would render the following message:
Satisfyr.Extensions+AssertException: "Hello world" Should Satisfy (s => s == "Hello World")
Strings differ at position 7.
Hello world
Hello World
______^____
Demo.cs(17,0): at Satisfyr.Tests.Demo.WhenTestingStringValue_ThenThrowsMeaningfullError()
Doesn't seem terribly fancy, right? How about checking multiple conditions and knowing exactly which one failed without having to write multiple "asserts" as is usual? In this next example, we're loading a Customer object from a repository and checking several of its properties in a single expression:
var customer = repository.Load<Customer>("1");
customer.Satisfies(c =>
c.Id == "1" &&
c.IsActive &&
c.DateOfBirth.Year >= 1900 &&
c.FirstName == "kzu" &&
c.LastName == "Cazzulino");
If the first name was wrong and actually entered as "Daniel" in the repository, here's the error message you'd see:
Satisfyr.Extensions+AssertException: Satisfyr.Tests.Demo+Customer Should Satisfy (c => c.FirstName == "kzu")
Strings differ at position 1.
Daniel
kzu
^_____
If the date of birth was the culprit, though, you would see something different but equally revealing as to which specific property did not pass the test:
Satisfyr.Extensions+AssertException: Satisfyr.Tests.Demo+Customer Should Satisfy (c => c.DateOfBirth.Year >= 1900)
Compared 1874 >= 1900.
More examples of lambda-based assertion like these can be found at
Fabio's project (he's the original creator of this idea. just replace his Satisfy with Satifies).
The unique feature of Satisfyr is that you don't need to take a binary dependency on us. You just download the Satisfyr source file corresponding to your unit testing framework of choice (or the generic one if not directly supported) and that's it. You get a minimal API surface (just the Satisfies extension method whenever you import the Satisfyr namespace) and an implementation that is native to your unit testing framework.
In addition, you can switch projects and continue using the same assertion idioms across all unit testing frameworks. Supports NUnit, MSTest and xUnit natively via the corresponding source file (i.e. Satisfyr.NUnit.cs).