I am attempting to write a test for a Web API method that uses HttpContext.Current.Request.Files
and after exhaustive searching and experimentation I cannot figure out how to mock for it. The method being tested looks like this:
[HttpPost]
public HttpResponseMessage Post()
{
var requestFiles = HttpContext.Current.Request.Files;
var file = requestFiles.Get(0);
//do some other stuff...
}
I realize that there are other questions similar to this, but they do not address this specific situation.
If I attempt to mock the context, I run into issues with the Http*
object hierarchy. Say I set up various mock objects (using Moq) like this:
var mockFiles = new Mock<HttpFileCollectionBase>();
mockFiles.Setup(s => s.Count).Returns(1);
var mockFile = new Mock<HttpPostedFileBase>();
mockFile.Setup(s => s.InputStream).Returns(new MemoryStream());
mockFiles.Setup(s => s.Get(It.IsAny<int>())).Returns(mockFile.Object);
var mockRequest = new Mock<HttpRequestBase>();
mockRequest.Setup(s => s.Files).Returns(mockFiles.Object);
var mockContext = new Mock<HttpContextBase>();
mockContext.Setup(s => s.Request).Returns(mockRequest.Object);
Attempting to assign it to the current context...
HttpContext.Current = mockContext.Object;
...results in a compiler error/redline because it Cannot convert source type 'System.Web.HttpContextBase' to target type 'System.Web.HttpContext'
.
I have also tried drilling into various context objects that come with the constructed controller object, but can't find one that a) is the return object of an HttpContext.Current
call in the controller method body and b) provides access to standard HttpRequest
properties, like Files
.
var requestMsg = controller.Request; //returns HttpRequestMessage
var context = controller.ControllerContext; //returns HttpControllerContext
var requestContext = controller.RequestContext; //read-only returns HttpRequestContext
It is also important to note that I cannot change the controller that I'm testing at all, so I cannot change the constructor to allow the context to be injected.
Is there any way to mock HttpContext.Current.Request.Files
for unit testing in Web API?
Aucun commentaire:
Enregistrer un commentaire