首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让FluentAssertions ShouldBeEquivalentTo在比较时检查类型?

如何让FluentAssertions ShouldBeEquivalentTo在比较时检查类型?
EN

Stack Overflow用户
提问于 2014-06-11 15:21:57
回答 3查看 3.7K关注 0票数 5

我有两个字典,我希望它们的内容不是相同的,因为字典包含不同类型的值。然而,下面的测试通过了

代码语言:javascript
复制
[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已经被转换为字符串,以便与第二个字典进行比较。

谢谢你的帮助

瑞秋

EN

回答 3

Stack Overflow用户

发布于 2015-07-10 13:47:54

但它们是等价的。这两个字典包含被认为是等效的相同的键和值。0L0可以转换为相同的类型,因此是等价的。另外,根据记录,ShouldBeEquivalentTo不会执行引用相等性检查。但是如果主体和期望是相同的客体,那么是的,它可以安全地假设它们也是等价的。

票数 1
EN

Stack Overflow用户

发布于 2015-05-13 22:36:46

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

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

如何使用FluentAssertions检查类型?

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

正确的FluentAssertions调用是“.Be().Be(...) as objectList.GetType().Should().Be(typeof(List<Classes.SomeListObject>));

票数 0
EN

Stack Overflow用户

发布于 2017-09-15 04:50:44

该库的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()进行类型转换。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24156925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档