Rob Bowman
Rob Bowman

Reputation: 8711

Using UrlHelper inside a controller breaks test

I'm new to MVC and TDD so please go easy on me!

I have an action that needs to redirect to another action. For this I'm constructing a base uri as follows:

UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
string baseURI = u.Action("PayPalAuth", "Order");

I've adapted this from PayPal's sample code (string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Order/PayPalAuth?";) but maybe I've not used the best method to come up with the baseURI for the target action?

Main problem is, when I call the action from an MSTest unit test I get a null exception on the ControllerContext.

What's the easiest way to solve this problem? I have found similar questions on SO but can't really follow them. I guess I may need to use a mocking framework but don't know where to start!

Upvotes: 0

Views: 269

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

What's the easiest way to solve this problem?

By mocking the ControllerContext. Here's an example of how this could be achieved: https://stackoverflow.com/a/32672/29407

Upvotes: 2

Related Questions