Blog Posts for: Unit Testing

Running a specific test with .NET Core and NUnit

24 November 2016

I converted the Unit tests for the Auth0.NET SDK to .NET Core. Currently the unit testing framework being used is NUnit, and NUnit 3 comes with a test runner for .NET Core. You can make use of it by configuring your project.json as follows: {"version":"1.0.0-*","dependencies":{"NUnit":"3.5.0","dotnet-test-nunit":"3.4.0-beta-3"},"testRunner":"nunit","frameworks":{"netcoreapp1.0":{"imports":"portable-net45+win8","dependencies":{"Microsoft.NETCore.App":{"version":"1.0.0-*","type":"platform"}}}}} The configuration above is current as of the writing of this blog post. Please refer to the NUnit 3 Test Runner for .NET Core GitHub Page to obtain the must up to date informaton on how to configure it.

Using Configuration files in .NET Core Unit Test Projects

24 November 2016

So another thing I came across while converting the Integration tests for the Auth0.NET SDK to .NET Core was that I had to make use of configuration files which specify the settings so the Integration test can talk with Auth0. Here are some of the basics which got it working for me… Add the configuration file First, add a client-secrets.json file to the Integration test project, e.g. {"AUTH0_CLIENT_ID":"...","AUTH0_CLIENT_SECRET":"..."} Configure the client-secrets.

Unit testing ASP.NET 5 Controllers with Entity Framework 7 In-Memory Database

24 November 2015

Introduction A number of months ago I wrote about how you could unit test with Entity Framework using a fake DbSet and NBuilder. It’s been almost 4 months since that post and I have done quite a lot of work using ASP.NET 5 in the meantime. So I thought I would write an updated version of that blog post, using ASP.NET 5 and Entity Framework 7. In this blog post I am going to show you a very simple scenario where I have a ProductsController class which displays a list of products, and also allows the user to select a product and navigate to a details page.

Unit testing controllers in ASP.NET 5 (MVC 6)

13 October 2015

Introduction Part of my journey in working with ASP.NET is figuring out how to replace the way in which I did things before with a more appropriate way of doing it in ASP.NET 5. One of these is figuring out how to test my controllers. Previously in ASP.NET MVC 5 I used the awesome FluentMVCTesting library, but currently it does not support ASP.NET 5, so I had to revert back to the old ways of doing things.

Unit Testing with NBuilder and NSubstitute using either a FakeDBSet or a mock DbSet

11 August 2015

In the previous blog post I showed how you can unit test with NBuilder and NSubstitute by using a FakeDbSet implementation. The thing is that we do not necessarily have to use a FakeDbSet but can also try and mock the DbSet. Let’s see how we can changes the implementation from last week’s blog post to mock DbSet instead. First thing to note is that a lot of examples for mocking the DbSet using other mocking frameworks such as Moq (such as this one) will demonstrate using DbSet and IQueryable, but it turns out that people using NSubstitute run into all sort of problems with this, as this SO question demonstrates.

Unit Testing with FakeDBSet and NBuilder

04 August 2015

Introduction I have mentioned in a previous blog post that I make use of NBuilder and Faker.NET to create test data for my application in development and testing scenarios. I also make use of NBuilder to set up scenarios for testing queries against my DbContext to ensure that the correct data is returned from a query. In this blog post I am going to demonstrate how you can test an ASP.

Reasons I like NSubstitute: Generate output based on input parameters

21 July 2015

NSubstitute is my current mocking framework of choice. It contains all the features I require in a mocking framework and is well maintained. I previously mentioned as one of my favourite Nuget packages for .NET and I also showed how you can mock abstract classes. In this blog post I want to talk a little bit about one of the many reasons I like NSubstitute, namely the ability to generate the output of a mocked function call based on the parameters passed into the function.

Unit testing with Dates

07 April 2015

Overview In a recent app we developed, there was a particular piece of code which monitored user accounts and would send out a notification before their subscription expires. There were various sets of notifications which we emailed to them; one a week before expiration, one 3 days before, one the day before expiration, and a final one once the subscription has actually expired. We needed to write unit tests for all these different scenarios to ensure that the correct emails go out on the correct dates.