Wednesday 12 August 2020

Setup C# Rest api with Unit testing

Lets start building a REST api; we are going to make a tic-tac-toe api with unit testing. To get started lets create a main folder were we will hold two projects, our source code and our unit testing project.


what we did above is create a main directory "tictactoe" then we added a webapi project as well as a testing project for our unit tests.

mkdir tictactoe creates our root folder 

donet new webapi -n -o source creates a webapi project named "" in a source folder

dotnet new xunit -n tictactoe.unitTests -o tests creates a xunit project named tictactoe.unitests in a tests folder.

we end up with something like the following

which is nice, but we still have a ways to go

next we are going to have to reference our game project in our unit tests project, since the unit test project will be testing all of our game artifacts.

dotnet add tests/tictactoe.unitTests.csproj reference source/

to ensure that we didn't mix up our references open up the tictactoe.unitTests.csproj and check that there is in fact a reference to the

<!-- tictactoe.unitTests.csproj file -->
<Project Sdk="Microsoft.NET.Sdk">



    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />

    <ProjectReference Include="..\source\" />


and sure enough there it is.

now we have one last optional thing that we can do, and in this case will do and that is add a solution file to our project, so that we can open our project in visual studio senior as well as just have a reference point that describes our project

dotnet new sln creates a solution file in our project root folder, now it defaults to the folder name if a -n parameter is not passed, in this case it's exactly what i wanted.

next we executed 

dotnet sln tictactoe.sln add source/ tests/tictactoe.unitTests.csproj

this references our two projects in our newly created solution file

then finally we ran 

dotnet sln list

just to confirm that we successfully referenced our two project in our solution file.

now of course if we would like we can now use the solution file to open our project in visual studio senior

however I am going to focus on visual studio code so lets open our project there instead, 

back in our terminal window in our root folder, that's where we configured our solution file type in 

code .

this will open the current directory in visual studio code

notice the .vs folder, this is a visual studio senior specific folder that was created automatically when we opened our solution file, we can just ignore it.

however now visual studio code is asking to create its own folder, just click yes on the bottom right modal, it will ask you which project to configure for launch choose the web api one. This will add a .vscode folder which holds visual studio code specific configurations for our project.