0

I'm running some basic unit tests on an MVC4 application. Within one of the main controller methods there is a call to Request.UserHostAddress.

However when I run my unit test it fails (Object null reference) which I presume is because the Request object has no context.

What's the bets thing to do in this situation. I read a suggestion about abstracting the Request object, but im not sure how, or whether this is the best approach?

Advice (and possible solution) would be appreciated.

Steve
  • 213,761
  • 22
  • 232
  • 286
higgsy
  • 1,991
  • 8
  • 30
  • 47

1 Answers1

0

You should be able to set the ControllerContext on your controller under test. This includes the RequestContext, which is what is used when you use Request.UserHostAddress. Then you just have to pass in a mocked HttpRequestBase.

driis
  • 161,458
  • 45
  • 265
  • 341