gpt4 book ai didi

c# - 如何使用 FluentAssertions 测试嵌套集合

转载 作者:行者123 更新时间:2023-11-30 12:30:19 27 4
gpt4 key购买 nike

我有以下规范

BidirectionalGraph Fixture = new BidirectionalGraph();

public void VerticesShouldBeAbleToAssociateMultipleEdges()
{
int a = 0;
int b = 1;
int c = 2;

Fixture.AddEdge(a, b);
Fixture.AddEdge(b, c);
Fixture.AddEdge(c, a);

Fixture.EdgesFrom(a).Should().BeEquivalentTo
( new []{a, b}
, new []{a, c});
}

EdgesFrom 是这样定义的

public IEnumerable<int[]> EdgesFrom(int vertex)

但是我的测试失败了

Result Message: Expected collection 

{{0, 1}, {0, 2}} to be equivalent to
{{0, 1}, {0, 2}}.

这对我来说不太有意义,因为它们显然是等价的。FluentAssertions 是否在比较集合时不起作用收藏品?

最佳答案

那是因为 collection.Should().BeEquivalentTo() 使用您的类型的默认 Equals() 实现来确保第一个集合中的每个项目都出现在第二个集合中的某个位置。你真正需要的是新equivalency feature我在 Fluent Assertions 2.0 中介绍的。不幸的是,我最近才意识到令人困惑的语法(collection.Should().BeEquivalentTo() 与 ShouldAllBeEquivalentTo())。

关于c# - 如何使用 FluentAssertions 测试嵌套集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com