Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.Querystring is null when use unit test

I want to create Nunit test case for ASP.NET MVC application. If the ASP.NET uses Request.QueryString or Request.Url. then the unit test case will return null reference exception since the Request.querystring will return the value if the control comes from UI only.

So can you please help on this to create a unit test for MVC controller which uses Httpcontext.Request attributes.

like image 405
Melody Avatar asked Feb 17 '26 14:02

Melody


1 Answers

Your unit test runs outside of the HttpContext and therefore you either need to mock the HttpContext or change your architecture to work around the problem, an example of this is shown below:

http://volaresystems.com/Blog/post/Dont-mock-HttpContext.aspx

like image 69
Burt Avatar answered Feb 20 '26 02:02

Burt