【发布时间】: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