SHARE:

You will never give up a unit test anymore with ExpectedObjects in .NET Core

Introduction

Let’s be honest, who has never abandoned a unit test because it was long and hard to test?
If you are reading this article it has probably happened to you, don’t be ashamed, because, in fact, I’am the first here who has abandoned when I was asked to test large objects, property by property…. ExpectedObjects is here to help us. As you should know, when you compare objects, you compare their reference, not the values of their properties…
ExpectedObjects helps us by testing an object to another one by its value, not its reference! Amazing! Let’s check that!

Scenario

We want to map this entity:

To this object:

As you can see, this object to map looks heavy (not the heaviest I have ever seen) , especially if there is a big collection of addresses. You will agree that we really don’t want to test property by property this object…..

ExpectedObjects in action

Don’t be scared! 🙂 Let’s download ExpectedObjects package and see how it works:

PM>  Install-Package ExpectedObjects -Version 2.3.4 

Now let’s create a test, arrange input values for the mapping method to test (AutoMapper here) and arrange the expected output. To this expected output let’s apply the method .ToExpectedObject() and compare it with the input object with the method .ShouldEqual(T).

Conclusion

Your test should pass :).

We significantly divided the time to make a unit test and it should increase your productivity!

Hope now you won’t give up anymore a unit test for a productivity and comfort reason, it won’t be painful anymore 😉

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: