Monthly Archives: April 2014

Getting HttpResponseException Details for Your WebAPI Unit Tests

For .NET unit testing assertions, I use Fluent Assertions.  Not only does it result in easier to read assertions, but it makes it easy to dig down in to the details of an exception.  This is particularly important for something like an HttpResponseException that can contain a complex object such as an HttpResponseMessage.  The HttpResponseMessage will have properties that you may want to test such as StatusCode and ReasonPhrase.

FluentAssertions documentation doesn’t (that I know of) show how to get  an object within an exception.  So this post is a reminder to myself of how to do just that.

Here is a simple ASP.NET WebAPI HTTP POST method that performs division.  It’s doing a check to safeguard against a caller attempting to divide by zero.  In the case the caller does attempt to divide by zero, an HttpResponseException is thrown.  The HttpResponseException contains an HttpResponseMessage with a 400 status code along with a reason.

public decimal DivideNumber(decimal dividand, decimal divisor)
    if (divisor == 0)
        // can't divide by zero
        throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "The divisor cannot be zero." });

    return dividand / divisor;

To test this, I’d like to verify that the HttpResponseException is thrown and it contains an HttpResponseMessage with an HttpStatusCode of 400 (Bad Request) and the contained reason phrase.  The variable named result is the HttpResponseException.  The result then contains a Response which is the HttpResponseMessage.  The Response then makes it possible to test the properties of the HttpResponseMessage as shown below:

public void DivideNumber_WithZeroDivisor_ShouldReturnHttp400()
    // Arrange
    const decimal dividand = 10.0m;
    const decimal divisor = 0.0m;
    var controller = new SampleController();

    // Act
    Action ax = () => controller.DivideNumber(dividand, divisor);

    // Assert
    var result = ax.ShouldThrow<HttpResponseException>();
    result.And.Response.ReasonPhrase.Should().Be("The divisor cannot be zero.");

Don’t forget to reference FluentAssertions in your unit test project.  It’s available on NuGet.

Tags: Filled Under: Programming Posted on: April 25, 2014

The Case of the Missing config File

While working on a code base that deploys to a CI environment, a “it work’s on my machine” moment occurred. In a brand new ASP.NET MVC 5 application that worked just fine locally, attempting to load any view resulted in a System.InvalidOperationException on the control environment. The exact exception was:

System.InvalidOperationException: The view at '~/Views/home/Index.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

In this particular CI environment, the deployment automation process has specific rules set up to exclude the copying of .config files. The reason for that exclusion isn’t all that important, but needless to say it’s a process that’s a work in progress. The root web.config contents are manually set on each deployment environment.

This was the first time this CI environment had an MVC deployed; it was previously set-up for ASP.NET web forms application. The root web.config was manually deployed, but it turns out the web.config in the Views folder that defines important config sections such as the one for the Razor view engine was forgotten. Once the Views web.config was deployed to the Views folder, this exception was resolved and the views began rendering.

Tags: Filled Under: Programming Posted on: April 17, 2014