我想测试以下代码行:
...
Bitmap uploadedPicture = Bitmap.FromStream(model.Picture.InputStream) as Bitmap;
...
图片是我的模型类型HttpPostedFileBase中的属性。 所以我想模拟一个HttpPostedFileBase属性进行单元测试:
model.Picture = new Mock<HttpPostedFileBase>().Object;
完全没问题。
现在我必须模拟InputStream,否则它是null:
model.Picture.InputStream = new Mock<Stream>().Object;
这不起作用,因为InputStream是只读的(没有setter方法):
public virtual Stream InputStream { get; }
是否有一种良好而干净的方法来处理这个问题? 一种解决方案是在我的单元测试的派生类中重写HttpPostedFileBase。 还有其他想法吗?
答案 0 :(得分:32)
你好:)我做了类似的事,
[TestInitialize]
public void SetUp()
{
_stream = new FileStream(string.Format(
ConfigurationManager.AppSettings["File"],
AppDomain.CurrentDomain.BaseDirectory),
FileMode.Open);
// Other stuff
}
关于测试本身,
[TestMethod]
public void FileUploadTest()
{
// Other stuff
#region Mock HttpPostedFileBase
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var files = new Mock<HttpFileCollectionBase>();
var file = new Mock<HttpPostedFileBase>();
context.Setup(x => x.Request).Returns(request.Object);
files.Setup(x => x.Count).Returns(1);
// The required properties from my Controller side
file.Setup(x => x.InputStream).Returns(_stream);
file.Setup(x => x.ContentLength).Returns((int)_stream.Length);
file.Setup(x => x.FileName).Returns(_stream.Name);
files.Setup(x => x.Get(0).InputStream).Returns(file.Object.InputStream);
request.Setup(x => x.Files).Returns(files.Object);
request.Setup(x => x.Files[0]).Returns(file.Object);
_controller.ControllerContext = new ControllerContext(
context.Object, new RouteData(), _controller);
// The rest...
}
希望这可以为您的解决方案提供一个想法:)
答案 1 :(得分:13)
我刚刚开始做类似的事情,并希望将以下内容添加到@ TiagoC13的答案中。
我正在测试的系统是我写的文件服务,其中一个要求是测试文件是否具有正确的尺寸。注意,硬编码的文件名。它作为我的测试项目中的文件夹和文件存在。文件的属性如下:构建操作:嵌入式资源和复制到输出目录:如果较新则复制(尽管复制始终应该正常工作)
构建项目时,testimage.jpg及其文件夹会添加到bin中,然后测试会找到它。
还要注意fileStream.Close();这会释放文件,因此您可以在同一套件中进行大量类似的测试。
希望这有帮助。
using Moq;
using NUnit.Framework;
using System.Web;
[Test]
public void IsValidFile() {
string filePath = Path.GetFullPath(@"testfiles\testimage.jpg");
FileStream fileStream = new FileStream(filePath, FileMode.Open);
Mock<HttpPostedFileBase> uploadedFile = new Mock<HttpPostedFileBase>();
uploadedFile
.Setup(f => f.ContentLength)
.Returns(10);
uploadedFile
.Setup(f => f.FileName)
.Returns("testimage.jpg");
uploadedFile
.Setup(f => f.InputStream)
.Returns(fileStream);
var actual = fileSystemService.IsValidImage(uploadedFile.Object, 720, 960);
Assert.That(actual, Is.True);
fileStream.Close();
}
答案 2 :(得分:9)
无需通过在磁盘上打开文件来创建流。实际上我认为这是一个非常糟糕的解决方案。可以在内存中轻松创建工作测试流。
var postedFile = new Mock<HttpPostedFileBase>();
using (var stream = new MemoryStream())
using (var bmp = new Bitmap(1, 1))
{
var graphics = Graphics.FromImage(bmp);
graphics.FillRectangle(Brushes.Black, 0, 0, 1, 1);
bmp.Save(stream, ImageFormat.Jpeg);
postedFile.Setup(pf => pf.InputStream).Returns(stream);
// Assert something with postedFile here
}