Take a look at the following examples:
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
[Fact] | |
public void IntTest() | |
{ | |
const int value = 5; | |
value.ShouldBe(5); | |
value.ShouldBeGreaterThan(4); | |
value.ShouldBeGreaterThanOrEqualTo(5); | |
value.ShouldBeInRange(0, 10); | |
value.ShouldBeLessThan(6); | |
value.ShouldBeOneOf(2, 5, 7); | |
} | |
[Fact] | |
public void StringTest() | |
{ | |
const string myString = "foo"; | |
myString.ShouldNotBeEmpty(); | |
myString.ShouldNotBe(null); | |
myString.ShouldNotBeNullOrEmpty(); | |
myString.ShouldStartWith("f"); | |
myString.ShouldEndWith("oo"); | |
myString.ShouldNotContain("bar"); | |
myString.ShouldBe("FOO", Case.Insensitive); | |
} | |
[Fact] | |
public void HelloWorldTest() | |
{ | |
var result = "Hello World".Split(new[] { ' ' }); | |
result.Length.ShouldBe(2); | |
result[0].ShouldBe("Hello"); | |
result[1].ShouldBe("World"); | |
} |
Another nice feature of Shouldly is that it can work with just about any Unit Test Framework (NUnit, XUnit, MSTest). Shouldly is easy to use and even simpler to install makes testing code much clearer.
It is available on GitHub and as a Nuget package.
For more details and advanced options please see the full Pluralsight course on the subject Better Unit Test Assertions with Shouldly.