ASP.NET Core Testing with XUnit and MoQ

by Rob Richardson



About Me

Rob Richardson is a software craftsman building web properties in ASP.NET and Node, Angular and React. He's a frequent speaker at conferences, user groups, and community events, and a diligent teacher and student of high quality software development. You can find this and other talks on https://robrich.org/presentations and follow him on twitter at @rob_rich.

.NET Core

.NET Core Features

  • Single compile works cross platform
  • Open source
  • Lighter weight
  • Remove legacy baggage
  • Small NuGet packages
  • Command-line tools
  • bin-deployable framework
  • Dependency Injection is baked in

.NET Core: didn't make the cut

  • In-memory compiling
  • project.json
  • Write project file yourself
  • Remove web.config

All these were or will soon be reversed

.NET Standard

.NET Classic

Source: http://andrewlock.net/understanding-net-core-netstandard-and-asp-net-core/

Portable Class Libraries

Source: http://andrewlock.net/understanding-net-core-netstandard-and-asp-net-core/

Portable Class Libraries

Each runtime is a fork

I must pre-select the runtimes I want

New runtime? I must recompile

.NET Standard

Source: http://andrewlock.net/understanding-net-core-netstandard-and-asp-net-core/

.NET Standard

Target Platform Name Alias
.NET Platform Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5
.NET Core netcoreapp 1.0
.NET Framework net 4.6.2
Universal Windows Platform uap 10.0
Windows win 8.1
Windows Phone wpa 8.1
Windows Phone Silverlight wp 8.1
Mono/Xamarin Platforms *
Mono *

Source: http://andrewlock.net/understanding-net-core-netstandard-and-asp-net-core/

.NET Standard

An evolution of PCL

Identifies a common feature set
for this version of .NET
in all environments


Detail: http://andrewlock.net/understanding-net-core-netstandard-and-asp-net-core/

Analogy (pseudocode): https://gist.github.com/davidfowl/8939f305567e1755412d6dc0b8baf1b7


Port of MVC onto .NET Core

All the features you know and love

  • Controllers
  • Razor
  • Action filters
  • Web API

New ASP.NET Features

  • Built on OWIN
  • Self-hosted
    or reverse-proxied from IIS
  • Web API rolled in
  • Tag Helpers
  • Bower and NPM

New features from .NET Core:

  • Cross-platform
  • Nano server
  • Fast startup
  • bin deployable framework

Not ported

  • Web Forms
  • SignalR (coming soon)
  • GDI+ (System.Drawing)
  • Control libraries?
  • Other dependencies?

New Entity Framework Features

  • Much better defaults
  • Supports much more databases
  • Entity Framework Migrations
  • ... including non-relational data stores
  • No edmx, no designer

Or bring your own data story through NuGet

Can I move to .NET Core?

  • Do I need cross-platform or nano server?
  • Are all my dependencies ported?
  • Am I ok with the pace of bleeding edge?
  • Do I want to go fast?

Dependency checker: icanhasdot.net

ASP.NET Core Demo


Testing pieces

  • Test harness:
    the thing that kicks it off
  • Test framework:
    the library for structuring tests
  • Assertion library:
    validating things, throw on fail
  • Mocking framework:
    swap out irrelevant things

Testing pieces


  • First one ported to .NET Core
  • Used by ASP.NET team to test ASP.NET Core


  • ASP.NET team built it to test MVC
  • Doesn't have magic strings (easier to refactor)
  • Arrange / Act / Assert style
    rather than record / playback


  • Not necessary, but makes it read much, much better
  • It just throws exceptions on fails
  • Code reads like a sentence
  • Failure message reads like a sentence

Testing Demos