Practical ASP.NET

Unit Testing AJAX Calls to an ASP.NET MVC Controller

Sometimes what you want to test is how your Action method behaves when it's invoked through an AJAX call. Here's how to mock up that call using Moq.

In an earlier column on using Moq to create unit tests, I looked briefly at using the ControllerContext object to create a Session object that you could configure for testing.

Recently, though, I faced a different problem: I needed a unit test for an Action method that returned a different result depending on whether it was called through a normal HTTP request or through an AJAX request. As it turns out, this was just the start of a series of AJAX-related tests I needed to create, but this was the start of all my subsequent solutions.

The code in the Action method looks something like this:

If Request.IsAjaxRequest Then
  Return Json(Orders)
End If
Return View(Orders)

Obviously, I needed to mock the Request object so that I could set its IsAjaxRequest property:

Dim req As New Mock(Of HttpRequestBase)

However, to set the IsAjaxRequest property, ASP.NET MVC looks for an X-Requested-With HTTP header set to XMLHttpRequest on the Request object. So, I set up my mock Request object to return that value:

req.Setup(Function(r) r("X-Requested-With")).Returns("XMLHttpRequest")

I also needed to mock the HttpContext object because, under the hood, it's the HttpContext object that's responsible for returning the Request object used in my Controller. I mocked it and tied its Request property to my mock Request object:

Dim hct As New Mock(Of HttpContextBase)
hct.Setup(Function(h) h.Request).Returns(req.Object)

Now I could instantiate the HomeController class I wanted to test and set its ControllerContext object (the ControllerContext is responsible for managing the HttpContext object). I did that by instantiating a ControllerContext class, passing my mock HttpContext object, an empty RouteData table (because I didn't use it in my Action method), and my HomeController object. I shoved the resulting ControllerContext class into my HomeContoller object's ControllerContext property:

Dim hc As New HomeController
hc.ControllerContext = New ControllerContext(hct.Object, New RouteData(), hc)

Finally, I could call the Action method I wanted to test:

Dim ac As ActionResult
ac = hc.GetCustomerOrders(2)

It turned out, by the way, that this was just the start of various AJAX mockings I needed to create. However, from this point on, all I needed to do was keep extending my mock Request object.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events