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:
- Rest server: WebAPI2
- QuickStart for WebAPI2 injection (jan 2014)
- IoC container: Microsoft Unity (from above sample)
- VisualStudio 2015 TDD and UnitTests: Quick Start: Test Driven Development with Test Explorer
- Mocking framework: Microsoft Fakes (using Microsoft.QualityTools.Testing.Fakes;)
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
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
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.
Noninvasive Unit Testing in ASP.NET MVC4 using Fakes.
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.
I came across FluentValidation in search for configuration validators, but the usage below was for validating Models.