Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock HttpPostedFileWrapper

I am trying to Mock the MVC object HttpPostedFileWrapper so I can test the "ContentType" and "InputStream" properties in particular. I set up my mock as so:

var mockPostedFile = new Mock<HttpPostedFile>();
var mockFileWrapper = new Mock<HttpPostedFileWrapper>(mockPostedFile);
mockFileWrapper.Setup(file => file.ContentType).Returns("application/pdf");
mockFileWrapper.Setup(file => file.InputStream).Returns(fileStream);

but I get this exception on the first line:

Type to mock must be an interface or an abstract or non-sealed class.

If I change HttpPostedFile to HttpPostedFileBase I get this exception when I call mockFileWrapper.Object

Can not instantiate proxy of class: System.Web.HttpPostedFileWrapper. Could not find a constructor that would match given arguments: Moq.Mock`1[System.Web.HttpPostedFileBase]

Does anyone know what I can do to pull this off?

like image 987
CodeGrue Avatar asked Feb 21 '23 11:02

CodeGrue


1 Answers

You should be mocking HttpPostedFileBase, not HttpPostedFileWrapper.

like image 83
SLaks Avatar answered Feb 27 '23 05:02

SLaks