Assertion Methods Available in Shouldly
Explore a wide range of assertion methods available in Shouldly, designed to enhance the readability and effectiveness of automated tests in xUnit. Learn how to validate values, check for nulls, compare object references, verify collections, and assert exceptions to improve test accuracy and clarity.
In this lesson, we’ll walk through a wide range of assertion extension methods supported by Shouldly. We will do so with the aid of the provided playground, which is set up with an excessive amount of assertions to demonstrate different ways of validating our test results:
namespace MainApp;
public class DataManager
{
private readonly List<string?> _collection;
public DataManager()
{
_collection = new List<string?>();
}
public List<string?> ReadAll()
{
return _collection;
}
public void DeleteAt(int index)
{
_collection.RemoveAt(index);
}
public void DeleteAll()
{
_collection.Clear();
}
public void Insert(string? newItem)
{
_collection.Add(newItem);
}
}
All our test methods in the above playground are in the DataManagerTestsWithShouldly.cs file under the MainApp.Tests folder.
Extension methods supported by Shouldly
All Shouldly extension methods can be found in the same library. As with any other assertion type, we can find an assertion method appropriate for any type of scenario.
Value assertions
These methods verify that a field, property, or variable contains an expected value.
The ShouldBe() method
This method is used to validate what the expected value should be. We have an example of it on ...