私はいくつかのレガシーコード用のユニットテストを作成しようとしています。私は、次の一つの方法を持っているFileDownloaderと呼ばれるテストする必要があり、クラスの 一つは:ユニットテストでHTTPResponseを擬似しました
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は使用できません。
ここに私が正確に何を言わなければならないのかわかりません。したがって、テストには何の主張もありません。 これは、この方法をテストする正しい方法です...アイデア。お知らせ下さい ?
おかげで、我々はsuccesslyそのような目的のためにHttpSimulatorを使用している私のプロジェクトで
ありがとう..私はHttpSimulatorを見てきましたが、私はまだ偽のHttpResponseを作成する方法に従うことができません...あなたはちょっと精巧にお考えください。 – Sennin