0

In c# .net how to read this data without using httpContext because when I am writing unit test cases httpContext is getting null.

int messageID = Convert.ToInt32(HttpContext.Current.Request.Params["messageId"]);  
var httpPostedFile = HttpContext.Current.Request.Files["attachmentdata"];
Draken
  • 3,049
  • 13
  • 32
  • 49
aditya
  • 1
  • 1
  • Please show us the entire method that that code exists in. – mjwills Aug 31 '18 at 04:18
  • Possible duplicate of [Mock HttpContext.Current in Test Init Method](https://stackoverflow.com/questions/4379450/mock-httpcontext-current-in-test-init-method) – mjwills Aug 31 '18 at 04:18
  • You need to learn about Mocking in Unit Test. HttpContext are not available when you run Unit tests so you need to create mocked HttpContext. There are frameworks available which can help you to create mocked objects in such cases. – Chetan Ranpariya Aug 31 '18 at 04:19
  • 1
    Instead of depending on the static methods in HttpContext how about depending on an abstraction e.g. IHttpContext. You can write a wrapper around HttpContext that implements IHttpContext and inject that when your application runs for real and inject a Moq when running in test space. – Nine Tails Aug 31 '18 at 13:49

0 Answers0