1

I'm writing a unit test for a controller in .Net Framework 4.5.2 but I'm getting a NullReferenceException when this line is hit:

return RedirectToAction<MyController>(action => action.Edit(itemId), "Something");

However, when I change it (and nothing else) to

return RedirectToAction("Edit", "Something", new { myId = itemId });

it works perfectly.

Any ideas why this would be?

1 Answer 1

1

Dependency injection configured in your project? That might be the reason.

For Unit test try to inject HttpContext in a mock controller object. Sample code is available here: https://stackoverflow.com/a/2497618/218408

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, dependency injection is done using Unity. I've tried injecting the HttpContext, but no luck.
Could you figure out on which line or object is throwing the null reference?
It is definitely on the return line posted in the original question, but it seems that all the objects are initialized correctly. I also tried stepping into it whilst debugging, but it simply throws the exception.
Did you fix this? If so, what was the cause?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.