lucianopereira86 / NetCore-XUnit

Simple example of unit tests with .Net Core and xUnit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

titulo

NetCore-XUnit

Simple example of unit tests with .Net Core and xUnit

Technologies

Objective

In this guide you will learn how to program automated tests in a .Net Core Web API by using xUnit . This project was developed with Visual Studio 2017 v15.

Behind The Code

The APITest solution inside the api folder is composed by two layers: Presentation and Test.
The Presentation layer contains the Web API structure such as Controllers, Models, Swagger, AutoMapper and FluentValidation.
The Test layer contains the unit tests.

code01

Presentation Layer

The UserController has two methods: Post and Put. Both follow the same steps:

  • Copy the view model to the domain model by using the AutoMapper.
  • Validate the domain model with FluentValidation and return a BadRequest in case of business exception.
  • In case of success, if there's an object to return, the AutoMapper copies the domain model to another view model.

The POST method sets the user id to 1 by simulating the auto-incrementation in a database when a new user is created.

code03

The PUT method does not return an object.

code04

Test Layer

When using the Test Manager, 4 unit tests will be shown inside the xUnit project.

code02

All the methods have the same behavior:

  • Create an AutoMapper object.
  • Instantiate the UserController and the User view model.
  • Run the Controller method and receive the result.
  • Check if the result is the same as expected.

TestPostUser_BadRequest (Post user with business exception)

code05

TestPostUser_Ok (Post user with success)

code06

TestPutUser_BadRequest (Put user with business exception)

code07

TestPutUser_Ok (Put user with success)

code08

How to Test the Project

In the Test Manager, click with the right button over UnitTestUser and debug or run the selected tests.

code09

All the tests will be successful.

code10

Now, let's change the UserPutVM's age from 33 to 15 and remove the id inside the TestPutUser_Ok method.

code11

Run the test again and there will be a failure.

code12

Why?
Because the PutUserValidation class (inside the UserController's Put method) was checking if the id was greater than zero.

code14

As it was using the same rules from the PostUserValidation class, the age had to be equal or greater than 18 as well.

code13

Swagger

Now, let's check how the business exception will be treated outside the xUnit.

Run the API and access the Swagger page.

swagger01

The Put method has the same example used in the TestPutUser_Ok method.

swagger02

Remove the id and change the age as done before.

swagger03

Execute the method and the business exception will be described like this:

swagger04

Conclusion

Although no database connection was present in this project, it was possible to test POST and PUT requests because the unit tests only needed to validate the results coming from the UserController's methods.

About

Simple example of unit tests with .Net Core and xUnit


Languages

Language:C# 100.0%