C# 如何使用FluentAssertions测试嵌套集合
我有以下规格C# 如何使用FluentAssertions测试嵌套集合,c#,collections,tdd,fluent-assertions,C#,Collections,Tdd,Fluent Assertions,我有以下规格 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);
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)
这对我来说不太有意义,因为它们显然是等价的。
比较集合时,FluentAssertions
是否不起作用
集合的类型?这是因为collection.Should().BeEquivalentTo()使用类型的默认Equals()实现,以确保第一个集合中的每个项都出现在第二个集合的某个位置。您真正需要的是我在Fluent Assertions 2.0中介绍的新内容。不幸的是,我最近才意识到语法混乱(collection.Should().beequivalento()与shouldallbeequivalento())。我天真地认为Should().beequivalento()将进行嵌套结构相等性测试。Should().BeEquivalentTo(depth:-1)表示无限深度,并让depth取默认值
1
,以保持向后兼容性。这很有意义。在内部,Should().BeEquivalentTo()应使用ShouldBeEquivalentTo()提供的相同结构等效API。
Result Message: Expected collection
{{0, 1}, {0, 2}} to be equivalent to
{{0, 1}, {0, 2}}.