我有一个实现 ICollection<SomeConcreteClass>
的类. NUnit 集合约束不将其识别为集合。
例如Assert.That( sut, Has.No.Member( someObjectOfTypeSomeConcreteClass ) );
抛出 System.ArgumentException : The actual value must be a collection
和Assert.That( sut, Is.Empty );
失败并显示为空 sut
.
那么集合什么时候是集合(根据 NUnit)?
堆栈跟踪:
System.ArgumentException : The actual value must be a collection Parametername: actual
at NUnit.Framework.Constraints.CollectionConstraint.Matches(Object actual)
at NUnit.Framework.Constraints.NotConstraint.Matches(Object actual)
MyTestFile.cs(36,0): at MyAssembly.MyTestFixture.MyTestMethod()
NUnit 2.4.3.0 会出现上述问题。我刚用 2.6 试过。 Is.Empty
现在有效,但是 Has.No.Member
仍然失败。它甚至不调用 Equals()
或 operator ==()
.它如何比较集合元素? RhinoMocks Arg<MyCollection>.List.Count( Is.Equal( 1 ) )
现在也失败了。
结论:
在 NUnit 2.4 中,集合约束需要实现非泛型 ICollection 才能将集合识别为集合(这回答了原始问题)。 IEnumerable 相等按预期工作。
NUnit 2.6(可能还有 3.0)等于 IEnumerable
即使 Equals
也会通过匹配元素检查 s被覆盖。这就是为什么如果元素是 IEnumerable
,则成员资格约束不起作用的原因。他们自己。这是一个已知问题 ( https://bugs.launchpad.net/nunit-3.0/+bug/646786 )。
有关详细信息,请参阅我自己的答案。
最佳答案
查看 NUnit 2.5.10 的源代码后:约束首先将给定集合转换为非泛型 IEnumerable
。
编辑:然后它在集合上运行 foreach() 并比较项目。所以 AFAICT 它应该可以工作。
您使用的是什么版本的 NUnit?
关于c# - 根据 NUnit 集合约束,什么使类成为集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9663310/