IoC with .NET 4.x / WebAPI 2

Last updated: 2017-02-10

With the myriad of IoC container, web frameworks, test and mocking frameworks it is hard to pick out a hand that makes you productive, when everytime you need one of the tools, then a new product or version has arrived and set the new standard.

Currently I am working with WebApi 2 so the natural thing is to let a WebApi 2 QuickStart do the selection for the hand:

But after checking other code in the department I chose to do as that, so they can feel at home. Only mocking framework differs:

using Microsoft.Practices.Unity; //IoC container
using Microsoft.VisualStudio.TestTools.UnitTesting; //MSTest UnitTest
using NSubstitute; //Mocking
using FluentAssertions;
using FluentValidation;
using FluentValidation.TestHelper;

But Microsoft Fakes have the concept of Shims which can be combined with use of Stubs (created with Nsubstitute). The shims are references to fake dll’s, which you would not inject using IoC in your SystemUnderTest (sut), like the framework you are using.

There are lots of extensions to NSubstitute. Some of them are

Using NUnit Assertions in MSTest tests

In the getting started of NSubstitute they are using NUnit assertions such as

Assert.That(someVal, Is.Equal.To(15))

I want to use the test-runner in the Test Explorer in Visual Studio. That one uses MSTest (calls tests decorated with [TestMethod()]). So if I want to assert with NUnit using Assert.That(), then I can do:

using Microsoft.VisualStudio.TestTools.UnitTesting; //MSTest
using NSubstitute;
using Assert = NUnit.Framework.Assert; //Overwrite Assert from MSTest with Assert from NUnit
using Is = NUnit.Framework.Is; //Include Is from NUnit

Thanks to Stéphane for the tip.

Warning: The above tip can lead to this exception:

threw exception: 
System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.

That can happen if you are using an Assert method, which exist in both MSTest and NUnit e.g.


and no other NUnit Asserts comes after.
You could rename the using Assert to using NAssert so there would be no doubt of what lib you are calling.

Testing MVC4

Noninvasive Unit Testing in ASP.NET MVC4 using Fakes.

Validating Configuration

In Unity I was looking for a ConfigurationValidation method as it is found in StructureMap (and AutoMapper).

Apparently there is no such method.

Instead I chose to throw an exception in default ctor, to indicate that default ctor is not missing (as client would else object about) and that the class is only intended to be constructed with injected dependencies.

Model Validation

I came across FluentValidation in search for configuration validators, but the usage below was for validating Models.

For WebAPI Core there are FluentValidation.WebApi and for WebAPI 2 based on .NET 4.x  there is also some validation help on using FluentValidation.

For UnitTesting there is FluentValidation.Validators.UnitTestExtension or FluentValidation Wiki.

The End.

One Comment

Add yours →

  1. In sut Use
    [assembly: InternalsVisibleTo(“SomeDll”)]
    for testing private classes and methods e.g. for verifying if methods was called.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: