Ever faced this situation: You have nice and easy controller method, say:

public ActionResult Demonstrate(string id) {   
    this.Repository.Demonstrate(id);   
    string url = this.Url.Action(MVC.Errors.Suck(id));   
    return this.Redirect(url); 
}

Then you go and create test for it (or created the test before, whatever suits you):

[TestMethod]   
public void Demonstrate_ValidInput_Redirects()   
{   
    // Arrange   
    var controller = new MyDemonstratingController();   
 
    // ... mock something here   
   
    // Act  
    controller.Demonstrate("kaboom");  
        
    // Assert  
    // ... test that everything was called  
}

…only to get NullReferenceException, since you did not set everything up that is needed by the UrlHelper class (controller’s Url property).

To satisfy the basic dependencies for a controller you can use MvcContrib’s TestControllerBuilder class like this:

TestControllerBuilder builder = new TestControllerBuilder();   
builder.InitializeController(controller);

InitializeController method adds mock implementations of the following:

  • HttpContext
  • HttpRequest
  • HttpResponse
  • HttpSession
  • Form
  • TempData
  • QueryString
  • ApplicationPath
  • PathInfo

There are also other helper methods than InitializeController in TestControllerBuilder, but for me InitializeController has been the most useful method from that library so far.

Using Protocol buffers v3 with ASP.NET core

This blog post gives a working Input and Output formatters for ASP.NET MVC to work with Google Protocol Buffers. Continue reading

Don't let techies design game user experience

Published on February 02, 2016

Using VS Team Services with go get

Published on January 19, 2016