在单元测试中模拟 HTTPResponse

发布于 2024-10-09 06:02:29 字数 1595 浏览 4 评论 0原文

我正在尝试为一些遗留代码创建单元测试。 我必须测试的类之一称为 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

Click this

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街道布景 2024-10-16 06:02:29

另一种测试方法是使用抽象基类,如 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。
当我测试上传/下载处理程序时,该功能似乎对我来说是一种补救措施:-)。

使用方法很简单。这个方法将会被测试覆盖。

public void Transmit(string fileName, HttpResponseBase response, DownloadFileType fileType, byte[] content)
{
...
// main logic.
...
}

对于真实的上下文,您只需创建一个存根并委托给可测试的方法,例如:

public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
{
   var requestWrapper = new HttpResponseWrapper(response);
   this.Transmit(fileName, requestWrapper, fileType, content);
}

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.

public void Transmit(string fileName, HttpResponseBase response, DownloadFileType fileType, byte[] content)
{
...
// main logic.
...
}

For the real context you can just create a stub and delegate to the testable method, like:

public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
{
   var requestWrapper = new HttpResponseWrapper(response);
   this.Transmit(fileName, requestWrapper, fileType, content);
}
别忘他 2024-10-16 06:02:29

在我的项目中,我们成功地将 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

椵侞 2024-10-16 06:02:29

谢谢大家..
实际上我应该使用假 HttpContext 的 Response
我创建了..而不是创建一个新的 Response 对象:

 downloader.Transmit("test.xls", HttpContext.Current.Response, DownloadFileType.Excel, bytes);

这有效!!!!

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 :

 downloader.Transmit("test.xls", HttpContext.Current.Response, DownloadFileType.Excel, bytes);

this works !!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文