Faking HTTP Context for your unit tests

While writing and testing ASP.Net MVC application, many times over I needed to simulate HTTP Request and presence of HttpContext.Current. I need this mostly when I try to test code that interacts with MVC framework, like MVC Action Filters or custom Model Binders or testing routes.

ASP.Net MVC 4 is quite good in this matter – most of the framework can be replaced by a mocked object and there is a minimum number of static calls or sealed objects. So you can do the HTTP Simulation, but usually it involves a lot of boiler-plate code and many mock-objects to be set-up.

Continue reading →

Test your constructors to be simple

In the previous blog post I wrote how it is important to have a simple constructor that only accepts dependencies. And violation of this rule cost me at least 12 hours of debugging and blocking issue for some developers in the team. Now I have a unit test to prevent this kind of problem in the future.

One of the suggestions in the comments was to use strict mocks and inject them into a controller. And don’t set up any kind of expectations on mocks. This way if dependencies are doing anything in the controller, they will fail the test.

Continue reading →

Mocking Dependency Injection.

All the cool kids do write their own dependency injection container at some point. Today I had to come up with one for testing purposes:

public class MockDependencyResolver : IDependencyResolver
{
    private readonly Dictionary<Type, object> collection;

    public MockDependencyResolver()
    {
        collection = new Dictionary<Type, object>();
    }

    public void Freeze<T>(object mockedObject)
    {
        collection.Add(typeof(T), mockedObject);
    }

    public object GetService(Type serviceType)
    {
        if (collection.ContainsKey(serviceType))
        {
            return collection[serviceType];
        }
        var message = String.Format("No object is registered for type {0}", serviceType.Name);
        throw new ArgumentException(message);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        throw new NotImplementedException();
    }

Pretty simple, really. Very simple. And here how I use it in tests:

[TestFixture]
public class SomeClassTests
{
    private MockDependencyResolver dependencyResolver;

    public void Initialize_GenericInstance_IsValid(String forkName)
    {
        var dependencyResolver = new MockDependencyResolver();

        // this is a part of MVC project, hence MVC internal static reference
        System.Web.MVC.DependencyResolver.SetResolver(dependencyResolver);

        // I'm using Moq framework
        var someService = new Mock<ISomeService>();
        dependencyResolver.Freeze<ISomeService>(someService.Object);
        someService.Setup(c => c.SomeDepenentOperation()).Verifiable();

        // do you tests with someService as a dependency
        // inside of the class under test somewhere I have 
        //  DependencyResolver.Current.GetService<ISomeService>(); that would return null
        //  without mocking of DI

        someService.VerifyAll();
    }
}