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?

Astrum97
  • 11
  • 1

1 Answers1

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

Rahatur
  • 3,000
  • 3
  • 32
  • 46
  • Yes, dependency injection is done using Unity. I've tried injecting the HttpContext, but no luck. – Astrum97 Apr 20 '20 at 12:36
  • Could you figure out on which line or object is throwing the null reference? – Rahatur Apr 20 '20 at 21:44
  • 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. – Astrum97 Apr 21 '20 at 07:46