我有两个字典,我希望它们的内容不是相同的,因为字典包含不同类型的值。然而,下面的测试通过了
[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已经被转换为字符串,以便与第二个字典进行比较。
谢谢你的帮助
瑞秋
发布于 2015-07-10 13:47:54
但它们是等价的。这两个字典包含被认为是等效的相同的键和值。0L和0可以转换为相同的类型,因此是等价的。另外,根据记录,ShouldBeEquivalentTo不会执行引用相等性检查。但是如果主体和期望是相同的客体,那么是的,它可以安全地假设它们也是等价的。
发布于 2015-05-13 22:36:46
这个问题已经有将近一年的历史了,但我是在寻找同一问题的答案时发现的。
请允许我稍微修改一下问题:
如何使用FluentAssertions检查类型?
FluentAssertions "ShouldBeEquivelentOf“是一个引用检查,用于查看对象是否”相同“(byRef)
正确的FluentAssertions调用是“.Be().Be(...) as objectList.GetType().Should().Be(typeof(List<Classes.SomeListObject>));”
发布于 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()进行类型转换。
https://stackoverflow.com/questions/24156925
复制相似问题