c# - 断言相等匿名类型

标签 c# .net unit-testing anonymous-types

我在声明两个匿名类型时遇到了问题。

到目前为止我做了什么

再一次,在发生断言异常后尝试再次测试相等性时:

1.

Assert.IsTrue(actionResult.Value.Equals(expectedActionResult.Value));

Expected: True But was: False

2.

Assert.AreEqual(actionResult.Value, expectedActionResult.Value);

Expected: <{ errorCode = -4, errorMessage = Invalid or Missing parameters within the request. }> (<>f__AnonymousType0'2[System.Int32,System.String]) But was: <{ errorCode = -4, errorMessage = Invalid or Missing parameters within the request. }> (<>f__AnonymousType0'2[System.Int32,System.String])

这是我创建真实和预期结果的地方:

var actionResult = _systemUnderTest.GetToken(null) as JsonResult;
var expectedActionResult = 
    new JsonResult(new
    {
        errorCode = (int)ErrorCodes.InvalidOrMissingParameters, errorMessage = ErrorCodes.InvalidOrMissingParameters.GetDescription()
    });

我错过了什么?

最佳答案

即使匿名类型在您的测试项目中可访问,但这并不意味着它们会在您编写new { ... } 时使用。

如果您查看 actionResult.Value.GetType()expectedActionResult.Value.GetType() 我强烈怀疑您会发现它们与不同的程序集。

在这种情况下,最简单的解决方法可能只是比较生成的 JSON。

关于c# - 断言相等匿名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45542180/

相关文章:

.net - 有什么方法可以将程序限制在一个CPU上而不关心是哪个CPU吗?

c# - 测试 Controller Get() 方法的最佳方法

unit-testing - 如何使用 Http 模块测试服务发送的 Angular 2 header ?

javascript - node.js - 在 mongodb 单元测试上应用 sinon

c# - Visual Studio 2008/2010 错误/问题列表

c# - C++/C#/Java视频分析题

c# - .NET 内存泄漏

c# - 如何在 C# 中从 XSD 检索 minoccurs 值?

.net - 仅在启动时找不到运行时的版本?

c# - 为什么 C# 中没有反向枚举器?