在单元测试中模拟 HTTPResponse
我正在尝试为一些遗留代码创建单元测试。 我必须测试的类之一称为 FileDownloader,它只有以下一种方法:
public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
{
response.Clear();
response.ClearHeaders();
response.ContentType = "application/xls";
response.AddHeader("content-disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(fileName));
response.BinaryWrite(content);
response.End();
response.Flush();
}
我不允许重构此代码(这将是理想的!)。
为了测试这一点,我决定根据下面的文章创建一个假的 HttpContext
这样我就可以在测试执行期间获得一个假的 HttpContext ,然而,伪造 HttpResponse 存在问题。
我的测试如下所示:
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
FakeHttpContext.CreateFakeHttpContext();
}
[Test]
public void ShouldTransmitHttpResponseInTheSpecifiedFormat()
{
FileDownloader downloader = new FileDownloader();
string path = "..\\..\\Fakes\\DummyDownloadReportsTemplate.xls";
byte[] bytes = ReadByteArrayFromFile(path);
downloader.Transmit("test.xls", new HttpResponse(new StringWriter()), DownloadFileType.Excel, bytes);
}
我将自定义创建的 HTTPResponse 对象传递给该方法。 当它到达“response.BinaryWrite(content)”行时,会引发以下异常:
System.Web.HttpException :使用自定义 TextWriter 时 OutputStream 不可用。
我不确定我到底应该在这里断言什么..因此测试中没有断言。 这是测试此方法的正确方法吗...任何想法。请指教 ?
谢谢
I'm trying to create unit tests for some legacy code .
One of the classes that I have to test is called FileDownloader which has just the following one method :
public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
{
response.Clear();
response.ClearHeaders();
response.ContentType = "application/xls";
response.AddHeader("content-disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(fileName));
response.BinaryWrite(content);
response.End();
response.Flush();
}
I'm not allowed to refactor this code ( which would have been ideal ! ).
To test this I decided to create a fake HttpContext based on the article below
With this I'm able to get a fake HttpContext during my test execution , however there are issues with faking the HttpResponse .
Here's how my test looks like :
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
FakeHttpContext.CreateFakeHttpContext();
}
[Test]
public void ShouldTransmitHttpResponseInTheSpecifiedFormat()
{
FileDownloader downloader = new FileDownloader();
string path = "..\\..\\Fakes\\DummyDownloadReportsTemplate.xls";
byte[] bytes = ReadByteArrayFromFile(path);
downloader.Transmit("test.xls", new HttpResponse(new StringWriter()), DownloadFileType.Excel, bytes);
}
I'm passing a custom created HTTPResponse object to the method.
This throws the following exception when it hits the "response.BinaryWrite(content)" line :
System.Web.HttpException : OutputStream is not available when a custom TextWriter is used.
I'm not sure what exactly should I be asserting here .. hence there no asserts in the test.
Is this the correct way to test this method ... any ideas . please advise ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种测试方法是使用抽象基类,如 HttpContextBase、HttpResponseBase 等。
http://msdn.microsoft。 com/en-us/library/system.web.httpcontextbase(v=VS.90).aspx
HttpContextBase 是 .NET 3.5 SP1、.NET 4.0 的一部分,可以作为 .NET 的单独包安装2.0。
当我测试上传/下载处理程序时,该功能似乎对我来说是一种补救措施:-)。
使用方法很简单。这个方法将会被测试覆盖。
对于真实的上下文,您只需创建一个存根并委托给可测试的方法,例如:
Another way of testing it is using abstract base classes like HttpContextBase, HttpResponseBase, etc.
http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase(v=VS.90).aspx
HttpContextBase is a part of .NET 3.5 SP1, .NET 4.0 and can be installed as a separate package for .NET 2.0.
When I was testing uploading/downloading handlers that feature had appeared a remedy for me :-).
Usage is simple. This method will be covered with test.
For the real context you can just create a stub and delegate to the testable method, like:
在我的项目中,我们成功地将 HttpSimulator 用于此类目的:
http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx
In my project we are successly using HttpSimulator for such purposes:
http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx
谢谢大家..
实际上我应该使用假 HttpContext 的 Response
我创建了..而不是创建一个新的 Response 对象:
这有效!!!!
Thanks all ..
Actually I should have been using the Response of the fake HttpContext that
I had created .. instead of creating a new Response object :
this works !!!!