I'm trying to create an instance of HttpPostedFile with
var obj2 = Activator.CreateInstance(
typeof(HttpPostedFile),
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new object[] { },
System.Globalization.CultureInfo.CurrentCulture
);
but I'm getting the error 'Constructor on type 'System.Web.HttpPostedFile' not found.'.
Is there another way to create an instance of HttpPostedFile or am I doing something wrong?
I've just had the same problem and solved it using Moq as follows
var postedfile = new Mock<HttpPostedFileBase>();
postedfile.Setup(f => f.ContentLength).Returns(8192);
postedfile.Setup(f => f.FileName).Returns("myfile.txt");
postedfile.Setup(f => f.InputStream).Returns(new MemoryStream(8192));
myObject.HttpPostedFile = postedfile.Object;
You might be able to use System.Web.HttpPostedFileBase instead. This has the same methods as HttpPostedFile but is designed for sub-classing. You can then use HttpPostedFileWrapperto pass in a real HttpPostedFile to a method expecting an HttpPostedFileBase.
These extra classes are in the System.Web.Abstractions assembly. ASP.NET MVC makes use of this (and other abstractions in the same assembly) to make unit testing easier. See the documentation on MSDN for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With