Thursday, August 24, 2017

Setting up XUnit, Moq, Fluent Assertions, Auto Mapper

We know why do we write Unit Test cases. If you are still not clear you can refer to below link
https://itsmevaibhav007.blogspot.in/2017/01/why-do-we-need-write-unit-test-cases.html where I have briefed about why we need to write unit test cases.

Before we jump into setting up the XUnit, Moq and Fluent Assertions. Let’s first understand what are these in brief

What is XUnit.?
Ans. XUnit is  a Unit test case framework for writing unit test cases against the code that is been written.

Why do we need to choose XUnit. When there are other Unit Test Cases Framework like NUnit, Visual Studio Tools  that are already available in the Market.?
Ans. There are multiple reasons choosing XUnit as Framework. Below are some points.

XUnit is Data Driven Test framework.
Written by the original inventor of NUnit v2
ASP.Net Core framework Unit testing is done by XUnit.

Where can I compare the features of XUnit with Other Frameworks.?
Ans. Here is a Link that will give you clear differences - https://xunit.github.io/docs/comparisons.html

Where can I learn more about XUnit.?
Ans. Here is the link where you can learn more about XUnit- https://github.com/xunit/xunit

What is Moq.?
Ans. Moq is a mocking framework for C#/.NET. It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called.

Why we need to use Moq.?
Ans. Moq framework will help us decouple the dependencies.

For ex say there are multiple Database call that are a part of your code and code intern calls the repository layer. Every time you run a unit test case it would refer to repository which would delay the whole purpose. With Moq we just do that. We Setup the data as if the code is calling the repository and giving the data back.

Where can I learn more about Moq.?
Ans. Here is the link where you can learn more about Moq - https://github.com/moq/moq4

What is Fluent Assertions.?
Ans. With Fluent Assertions, the assertions look beautiful, natural and most importantly, extremely readable.

Why do we need to use Fluent Assertions.?
Ans. By using fluent assertions you could write assert as you say it.

For ex if you want to assert the following the count in the list should be equal to 4.

In Normal case scenario you would write Assert.Equal(employee.count,4);
With Fluent assertion you would write employee.Count.ShouldBeEquivalentTo(4).

Where can I learn more about Fluent Assertions.?
Ans. Here is the link where you can learn more about the Fluent Assertions - http://fluentassertions.com/

What is Auto Mapper.?
Ans. Auto Mapper is a simple little library that will help us to get rid of Old style of mapping one object to another.

Why do we need to use Auto Mapper.?
Ans. If there is situation where you would want to map one object consisting 20 properties with another object consisting 20 more properties the code would look more complicated as you would do old style mapping. One to One. Auto mapper help us get rid of the same.

Where do I learn more about Auto Mapper.?
Ans. Here is the link where you can learn more about the Auto Mapper - https://github.com/AutoMapper/AutoMapper

I have also written how to setup Auto Mapper using reflection you could read more on that in this link - https://itsmevaibhav007.blogspot.in/2016/11/adding-auto-mapper-500-profile.html

Happy Coding :) :) :)