Skip to content

Instantly share code, notes, and snippets.

@rdsimes
Last active August 8, 2016 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdsimes/144912ca5ca60c9f43f2cdd5aaa9c7f0 to your computer and use it in GitHub Desktop.
Save rdsimes/144912ca5ca60c9f43f2cdd5aaa9c7f0 to your computer and use it in GitHub Desktop.
o

Unit testing, Integration testing & Dependency Injection with Microsoft .NET Core

We'll walk through the basics of setting up a test projects using dotnet test, and take you through including Microsoft's dependency injection framework and writing Integration test.

Project setup

You'll want to set up three projects - a class library and two test projects like so:

mkdir WeatherConverter
cd WeatherConverter

mkdir src
mkdir src/WeatherConverter

mkdir test
mkdir test/Unit
mkdir test/Integration

Then you'll need to create a global.json file in the project root as follows:

{
    "projects": [
        "src",
        "test"
    ]
}

Our class library is going to convert temperatures from Celcius to Farenheight and will also be able to look up the current temperature given a city - naturally we want to build this using TDD!

Let's start by creating some unit tests - we'll CD into the Unit test folder and use the dotnet cli to create an xunit test project:

cd test/Unit
dotnet new -t xunittest

Then build and run the tests with:

dotnet restore
dotnet test

Hopefully this should build and run the one example test - lets change this so we can start testing the Temperature class we want to write:

mv Tests.cs Temperature_AsCelciusShould.cs

Then I'll write my first test as follows:


using System;
using Xunit;

namespace Tests.Unit
{
    public class Temperature_AsCelciusShould
    {
        [Fact]
        public void Return0WhenGiven0() 
        {

            var temperature = new Temperature(0);

            decimal actual = temperature.AsCelcius();


            decimal expected = 0;
            Assert.Equal(actual, expected);
        }
    }
}

Then we'll create our Temperature class and try to get the solution building and the test passing:

src/TemperatureCalculator/Temperature.cs:
-------

using System;

namespace TemperatureCalculator
{
    public class Temperature
    {
        public Temperature (decimal temperatureCelcius){
        }
        
        public decimal AsCelcius()
        {    
            throw new NotImplementedException();
        }
    }
}

You'll also need to reference the class library project from the unit test project by adding the reference in to test/Unit/project.json

...

"dependencies": {
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24",
    "TemperatureCalculator": {
      "target": "project"
    }
  },
  ...

Then you'll need to run:

dotnet restore

to resolve the dependency before running the tests. Now we can go about fixing the test and adding some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment