Becoming a software developer – episode XII

Becoming a software developer – episode XII

Welcome to the twelfth episode of my course “Becoming a software developer” in which we will write tests, both unit and integration (end-to-end) for our application.

All of the materials including videos and sample projects can be downloaded from here.
The source code repository is being hosted on GitHub.


 

Scope

  • Unit tests
  • Integration tests

Abstract

Unit tests

You can read moe about testing here (the episode VII of this course), so I will not go into the details of this practice here. Instead, I want to tell you what is needed in order to start writing tests for the Passenger app. At first, I decided to use xUnit instead of NUnit, mostly due to the fact that I had some issues related to running NUnit tests after the latest update of the .NET Core framework to the version 1.1.
For the starters, including the following dependencies within the Passenger.Tests.csproj file:

And let’s write a very basic tests – create a new directory called Services and add a new class UserServiceTests containing the following code:

Do not forget about adding the required “using” to the missing namespaces. Eventually, run the dotnet test command and that’s it, your first unit test shall successfully pass!

Integration tests

Unit tests are easy, so what about creating sophisticated integration tests that will execute the real HTTP calls on our API? It could be done twofold – the first way is to run the API using dotnet run and write tests using e.g. HttpClient in order to send requests and validate them by using particular assertions.

However, there’s’ also another way, much cooler than that. Thanks to the ASP.NET Core framework, you can run the whole API in the memory and perform the integration tests this way, which is really cool. You can find more details here, but this is how could it look like.
At first, include the following dependencies within the Passenger.Tests.EndToEnd.csproj file:

And let’s write the actual tests – create a new directory named Controllers and add a new class UsersControllerTests. Having done that, implement the following tests:

As before, make sure you do not forget about adding the required “using” to the missing namespaces. Finally, run the dotnet test command and that’s all!

Next

In the next episode, we’ll talk implement the Command Handler pattern and make use of the external Autofac (which is a powerful IoC container) in order to achieve such goal.

5 Comments Becoming a software developer – episode XII

  1. Pingback: Dew Drop - April 17, 2017 (#2459) - Morning Dew

  2. Tk

    Rzeczywi艣cie artyku艂 jest bardzo dobry i nawet chcia艂em spr贸bowa膰 test贸w integracyjnych tyle, 偶e w pe艂nym .net i OWIN. Samo uruchomienie aplikacji pod testy nie jest specjalnie trudne, bo sk艂ada si臋 do napisania kilka linijek. Problemem jest operowanie na entity framework w pami臋ci, z kt贸rym nie mog艂em sobie poradzi膰. Mo偶e jest kto艣 w stanie pom贸c poprzez link do artyku艂u lub mo偶e sam zna rozwi膮zanie, bo p贸ki co nie wiem gdzie szuka膰.
    Pozdrawiam

    Reply
      1. Tk

        No tak EF Core posiada, tylko ja dzia艂am pod EF 6(Poprostu 艣ledz膮c kurs ucz臋 si臋 te偶 .net 4.6, z kt贸rego nie czuje si臋 jeszcze pewnie) i tutaj pojawia si臋 艣ciana(przynajmniej dla mnie) bo dostaje jedynie na czerwono 3 wyj膮tki 1 z entity i 2 z mscorlib i status code 500.

        Reply

Leave A Comment

Your email address will not be published. Required fields are marked *