1
votes

Here is the controller's return statement:

var url = Url.Action("method", "controller", new { id = id }) + "#anchor-tag";
return new RedirectResult(url);

So, in my test method, I need to assign the Controller's (sut) UrlHelper so I don't get a null reference exception. I'm using FakeItEasy to mock the HttpContext:

sut.Url = new UrlHelper(new RequestContext(A.Fake<HttpContextBase>(), new RouteData()), new RouteCollection());

this seems to be working fine, but when I want to prove that the controller's action method has redirected to the correct method and controller:

var redirectResult = sut.DoSomething(input);

I can't extract the RouteValues like I can when the controller action returns a RedirectToRouteResult. aka:

Assert.That(redirectResult.RouteValues["id"], Is.EqualTo(Id));

b/c RedirectResult does not allow me to access those variables. Also, if I try to cast it:

var redirectResult = (RedirectToRouteResult)sut.DoSomething(input);

I get a casting error.

I need to be able to test the controller is redirecting to appropriate place, but I can't do that b/c Url.Action(...) must work internally with other items such as RouteData, RouteCollection, etc...

anyone have any idea how I can pre-populate the "route" data I need in my test using the ControllerContext:

sut.ControllerContext = new ControllerContext(requestContext, sut);

in order to get Url.Action to spit the correct values back to me in the test?

Thanks! Mike

1

1 Answers

2
votes

If i understand your question correctly, you want to test the url returned from returning RedirectResult.

You can simply do something like this:

var result = (RedirectResult)aec.Result;

And then result.Url.AbsoluteURI contains the string value that you can parse