asp.net - 无法在单元测试中检查响应 header

标签 asp.net unit-testing nunit

我有一个 Http 处理程序的单元测试。在其中,我创建了一个 HttpResponse 对象并将其传递给我的 Http 处理程序的方法之一。

我的一项测试尝试验证响应 header 是否已正确设置:

Assert.AreEqual( "gzip", response.Headers["Content-Encoding"]);

但是,Headers 属性会引发 PlatformNotSupportedException 并显示消息“此操作需要 IIS 集成管道模式”

奇怪的是,据我了解,该异常与设置响应 header 有关 - 不读取它们。我正在使用 TDD,所以我还没有在任何地方设置 header ,但我仍然遇到了异常。

为什么我会收到此异常?是否有好的或更好的方法来对响应 header 进行单元测试?

最佳答案

来自Response.Headers documentation :

Remarks

The Headers property is only supported with the IIS 7.0 integrated pipeline mode and at least the .NET Framework 3.0. When you try to access the Headers property and either of these two conditions is not met, a PlatformNotSupportedException is thrown.

基本上,除非您在这些条件下运行,否则您甚至无法尝试访问它。

如果我是你,我会为你的处理程序创建一个构造函数,它接受 HttpContextBase对象和use a mock为了正确测试您的 header 。

关于asp.net - 无法在单元测试中检查响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947471/

相关文章:

javascript - Microsoft Stack 中的 D3 示例

unit-testing - 我可以将 Watin 配置为在测试完成时让浏览器窗口保持打开状态吗?

c# - 如何使用相同的通用方法来验证不同类型

unit-testing - 如何使用 JUnit 在 android 中对 Firestore 进行单元测试?

javascript - 使用 node-mocks-http 测试没有发出事件的服务

android - android开发中应该做单元测试吗?

c# - 如何测试方法是否返回预期的数据类型?

c# - Azure Devops Visual Studio 测试任务无法发现具有 TestCaseSource 属性的 Nunit 测试

c# - 从 SqlCommand 对象发送什么 SQL

c# - 尝试在 ASP.NET 页面上显示 Crystal 报表时出错