Monday, April 09, 2018

ASP.Net Core 2 with Postgresql on Linux Part 6: Setting up Unit Testing

In In Part I, I discussed the rationale for porting a ASP.Net Core 2 app to my Linux system replacing SQL Server with Postgresql and place it on Heroku. 

With Part II, we transferred the SQL Server data to Postgres.


In Part III, I create the ASP.Net MVC Core 2.x app, from which we will proceed, and get Entity Framework Core installed.


Then in Part IV, I look at the configuration differences between the ASP.Net MVC app that uses SQL Server and the new app, in preparation of porting the old app to use Postgresql.

Part V, we compared the differences in both the projects' structures and ported the existing project code, starting with the data model layer, to the new project. In addition to this, we got the jquery.countdown jquery plugin installed, that is used to implement a JavaScript timer. More on that in a later article. Finally, we used the dotnet ef dbcontext scaffold command that builds a DbContext and entity types for out Postgresql database. And, oh yes, we added the project to github, which frankly should have been done day one.

In this post, Part VI, it would be best to get a unit testing framework in place
in order to test both the new and future code.

First, let's create a new unit test project. I have found that it is best to separate the csproj files so I have created a new folder. So from a separate folder than the myApp folder I issued the following command:


 $ dotnet new classlib -n UnitTests  


Here is the output



Using the new Multi-root workspace feature, I was able to add the new UnitTests project to thew view in VS Code:


Next, within the new UnitTest project folder,  I issued the following in the unit test project directory:


 $ dotnet add package NUnit  


After that:


 $ dotnet add package Microsoft.NET.Test.Sdk  


Then:


 $ dotnet add package NUnit.Console  




The final command to get all the NUnit packages installed was:


 $ dotnet add package NUnit3TestAdapter  


I issued the dotnet restore to make sure that everything was up to date. When doing that I noted the following error:



I then noted that the target framework was not the standard and not the core framework:



After updating the TargetFramework setting to netcoreapp2.0 and running dotnet restore, it was good.

One thing I noted with working with the multi-root workspace feature, the C# extension settings did not take effect until I closed VS Code, opened the new UnitTest project in VS Code by itself, rebuilt the project, closed it, and then re-opened the multi-root workspace.

Next, step was to create an NUnit test to simply test that the NUnit packages are setup properly. This seemed appropriate:



I then invoked the dotnet test command and all is well:



In Part VII, we will get the Service layer setup in order to unit test it as well as the Data and Model tiers.


No comments: