C# 单元测试返回匿名类型的响应重定向路由

C# 单元测试返回匿名类型的响应重定向路由,c#,unit-testing,tdd,rhino-mocks,anonymous-types,C#,Unit Testing,Tdd,Rhino Mocks,Anonymous Types,我正在AspNet MVC应用程序中使用HttpResponseBase对象测试HttpModule,正如 在我的测试代码中,我有: context.Response.RedirectToRoute(new { Controller = "C1", Action = "A1" }); 在我的单元测试中(在单独的测试项目中使用Rhinomock),我可以使用以下方法获得传递给此方法的实际参数: var actual = this.ResponseMock.GetArgumentsForCalls

我正在AspNet MVC应用程序中使用HttpResponseBase对象测试HttpModule,正如

在我的测试代码中,我有:

context.Response.RedirectToRoute(new { Controller = "C1", Action = "A1" });
在我的单元测试中(在单独的测试项目中使用Rhinomock),我可以使用以下方法获得传递给此方法的实际参数:

var actual = this.ResponseMock.GetArgumentsForCallsMadeOn(r => r.RedirectToRoute(new { Controller = "dummy", Action = "dummy" }),
            options => options.IgnoreArguments())[0][0];
我可以在测试中创建预期参数,如下所示:

var expected = new {Controller = "C1", Action = "A1" };
当我运行这个测试时,一切看起来都很有希望。如果我把“实际”和“预期”都放在监视窗口中,我可以看到它们的值看起来相同,它们的匿名类型也一样

但是,当涉及到断言时,VisualStudio无法看到实际的属性,因此我无法实际键入断言。如果我键入以下内容:

actual.Controller =
Visual Studio只是说:

无法解析符号“控制器”

我已经尝试了推荐用于强制转换为匿名类型的解决方案(例如,但这只会引发以下异常:

{[A]f_uanonymoustype0
2[System.String,System.String]不能强制转换为[B]f_uanonymoustype0
2[System.String,System.String].A类型源自位置“^MyAssemblyUnderTest.dll^”的上下文“Default”中的“^My Assembly UnderTest.dll^,版本=1.0.0,区域性=neutral,PublicKeyToken=null”。B类型源自位置“^MyTestAssembly.dll^”的上下文“Default”中的“^My Test Assembly^,版本=1.0.0,区域性=neutral,PublicKeyToken=null”

我曾尝试使用反射将属性从“实际”复制到新的匿名类型,但也失败了。但愿我知道我的监视窗口是如何做到这一点的

所以,我想要的是:

  • 理想情况下,对我的“actual”执行断言的首选方法是显示其属性与预期一致
  • 或者,一种将我的“真实”转化为我能阅读的东西的方法
提前谢谢


格里夫最终解决了这个问题……答案是关于stackoverflow(还有什么地方?)

总结如下:

测试类别(HTTPM模块组件)

测试类(HttpModuleTest组件)

那么您就可以执行您的断言了

context.Response.RedirectToRoute(new { Controller = "C1", Action = "A1" });
var actual = this.ResponseMock
    .GetArgumentsForCallsMadeOn(r => 
        r.RedirectToRoute(new { Controller = "dummy", Action = "dummy" }),
            options => options.IgnoreArguments())[0][0];

var controllerProperty = properties.Find("Controller", false);
var actionProperty = properties.Find("Action", false);

var controllerProperty = properties.Find("Controller", false);
var actionProperty = properties.Find("Action", false);

string controllerValue = controllerProperty.GetValue(actual).ToString();
string actionValue = actionProperty.GetValue(actual).ToString();