The ShouldlyExtension.IEnumerable is a C# library that provides extension methods for the Shouldly assertion library. It is designed to simplify the process of testing collections in unit tests.
The library contains four extension methods that allow you to test whether the first or last item in a collection is equivalent to a given object. These methods include:
This method asserts that the last item in the collection is equivalent to a given object.
This method asserts that the last item in the collection, after being sorted by a key, is equivalent to a given object.
This method asserts that the first item in the collection is equivalent to a given object.
This method asserts that the first item in the collection, after being sorted by a key, is equivalent to a given object.
In addition, these methods can raise a NullReferenceException if the collection is empty. Here's an example of how to use ShouldlyExtension.IEnumerable:
make model for test with some properties:
public class MyClass { public int Id { get; set; } public string Name { get; set; } }
create unitTest :
public void TestMethod() { var myList = new List { new MyClass { Id = 1, Name = "Test 1" }, new MyClass { Id = 2, Name = "Test 2" }, new MyClass { Id = 3, Name = "Test 3" } }; // Assert that the last item in the list is equivalent to a given object myList.ShouldBeEquivalentToLastItem(new MyClass { Id = 3, Name = "Test 3" }); // Assert that the last item in the list, after being sorted by a key, is equivalent to a given object myList.ShouldBeEquivalentToLastItem(x => x.Id, new MyClass { Id = 3, Name = "Test 3" }); // Assert that the first item in the list is equivalent to a given object myList.ShouldBeEquivalentToFirstItem(new MyClass { Id = 1, Name = "Test 1" }); // Assert that the first item in the list, after being sorted by a key, is equivalent to a given object myList.ShouldBeEquivalentToFirstItem(x => x.Id, new MyClass { Id = 1, Name = "Test 1" }); }
more examples for assert :
// Assert that the last item in the list is equivalent to a given object people.ShouldBeEquivalentToLastItem(new Person { Name = "Charlie", Age = 35 });
with desc order :
// Assert that the last item in the list, after being sorted by age, is equivalent to a given object people.ShouldBeEquivalentToLastItem(x => x.Age, new Person { Name = "Charlie", Age = 35 }, OrderType.Descending);
with asce order :
// Assert that the first item in the list, after being sorted by name, is equivalent to a given object people.ShouldBeEquivalentToFirstItem(x => x.Name, new Person { Name = "Alice", Age = 25 }, OrderType.Ascending);
Contributions to ShouldlyExtension.IEnumerable are welcome! If you find a bug or have an idea for a new feature, please open an issue and describe your problem or suggestion. If you would like to contribute code, please fork the repository and submit a pull request. All contributions should include tests to ensure that they work as expected.
ShouldlyExtension.IEnumerable is licensed under the MIT License.