C# Unit Testing Objects, IEnumerables, and Dates

C# Unit Testing Objects, IEnumerables, and Dates

In a previous post we looked at an introduction to Unit Testing, I want to expand on that and start looking at different data types. To do this I added some additional content to the UserService class.

C#

Date

Using Fluent Assertions gives us a large number of Test, a complete list can be found here. I have used some of these to test the Creation Date method within the User Service.

C#

Object

This time were going to test an Object, to do so I’m going to re-use the GetUserById method again. However we need to clean something up first, in each Test Case we New’d up the userService. To keep this cleaner we will create a private member and move the new to the constructor, and then update the already created tests to use the private member.

C#

Now we can test the Object, here we have two test;

  • BeofType<TypeName> – Validates that the correct type is returned.
  • BeEquivalentTo – Does a Parity Check between two objects, this validates that all the property values are identical.
C#

IEnumerable

With an IEnumerable we can again check the Type, but we also have content checks to ensure that the contents of the IEnumerable are correct.

C#