【问题标题】:How can I make FluentAssertions ShouldBeEquivalentTo check for type when comparing?比较时如何使 FluentAssertions ShouldBeEquivalentTo 检查类型?
【发布时间】:2014-08-01 03:45:25
【问题描述】:

我有 2 个字典,我希望内容不相等,因为字典包含不同类型的值。但是下面的测试通过了

[Scenario]
public void DictionariesWithDifferentTypesShouldBeEquivalent(
    Dictionary<string, object> firstDictionary, 
    Dictionary<string, object> secondDictionary)
{
    "Given a dictionary"
        .f(() => firstDictionary = new Dictionary<string, object> 
                    {
                        { "latency", 0 },
                        { "errorMessages", new string[0] },
                        { "lastChanged", new DateTime(635272310930829706) },
                        { "query", new string[0] },
                        { "items", new string[] { "foo", "bar" } },
                        { "name", "Bob" },
                        { "number", 3 },
                        { "updateInterval", 10 },
                    });

    "And a second dictionary with same values but of differing types"
        .f(() => secondDictionary = new Dictionary<string, object> 
                    {
                        { "latency", 0L },
                        { "errorMessages", new object[0] },
                        { "lastChanged", new DateTime(635272310930829706) },
                        { "query", new string[0] },
                        { "items", new string[] { "bar", "foo" } },
                        { "name", "Bob" },
                        { "number", 3 },
                        { "updateInterval", "10" },
                    });

    "When I check for equivalency"
        .f(() => { });

    "Then the dictionaries should be equivalent"
        .f(() => firstDictionary.ShouldBeEquivalentTo(secondDictionary));
}

如果这是预期的行为,我该如何设置流畅的断言规则来检查类型是否匹配?

我已经使用 MatchingRule 和 AssertionRule 进行了调查,但在这两种情况下,我似乎都无法访问主题的原始类型和预期。似乎主题已经转换为预期的类型。即在上面的示例中,第一个字典中的 updateInterval 已经被转换为字符串以与第二个字典进行比较。

感谢您的帮助,
雷切尔

【问题讨论】:

  • 这是设计使然。默认情况下,它将进行递归结构比较,忽略集合中项目的顺序,并尝试将实际值转换为预期值。它应该在哪个特定路径上失败?
  • 如果您不回答我的问题,我无法为您提供解决方案。
  • 有没有办法强制比较考虑类型?例如,WithStrictOrdering 可用于要求嵌套集合具有相同的顺序,从而更改默认行为。有没有办法说“嵌套对象需要等价,包括它们的类型”,这样如果你碰巧有一个没有额外属性的派生类,它们就会被认为是不等价的?

标签: c# fluent-assertions


【解决方案1】:

但它们是等价的。两个字典都包含被视为等效的相同键和值。 0L0 可以转换为相同的类型,因此是等价的。并且为了记录,ShouldBeEquivalentTo 不做参考平等检查。但是如果 subjectexpectation 是同一个对象,那么是的,它可以安全地假设它们也是等价的。

【讨论】:

    【解决方案2】:

    [这个问题已经有将近一年的历史了,但是我在寻找同一个问题的答案时发现了它]

    请允许我稍微改变一下问题:

    如何使用 FluentAssertions 检查类型?

    FluentAssertions "ShouldBeEquivelentOf" 是一个参考检查,用于查看对象是否“相同”(byRef)

    正确的 FluentAssertions 调用只是“Should().Be(...) 如objectList.GetType().Should().Be(typeof(List&lt;Classes.SomeListObject&gt;));

    【讨论】:

      【解决方案3】:

      库的版本5.0.0-beta.1 现在支持Should().BeEquivalentTo(),无需开箱即用的类型转换。

      https://github.com/fluentassertions/fluentassertions/releases/tag/5.0.0-beta.1 https://github.com/fluentassertions/fluentassertions/pull/616

      您可以使用WithAutoConversion() 选择进行类型转换。

      【讨论】:

      • 我正在尝试比较对象的图形,并希望它只使用属性值和类名来匹配。 FA 通过 new AbsoluteLink("path").Shold().BeEuivalentTo(new RelativeLink("path")) 我希望它说类名不匹配
      猜你喜欢
      • 2016-12-29
      • 2016-01-28
      • 2013-04-15
      • 2014-11-12
      • 2017-03-21
      • 2017-06-15
      • 2019-07-14
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多