
[Fact]public voID Sometest(){ // Do something in Arrange and Act phase to obtain a collection List<int> actual = ... // Now the important stuff in the Assert phase var expected = new List<int> { 42,87,30 }; Assert.Equal(expected.Count,actual.Count); foreach (var item in actual) Assert.True(expected.Contains(item));} 有没有更简单的方法来实现这一点在xunit.net?我不能使用Assert.Equal,因为这个方法检查项目的顺序在两个集合中是否相同.我看过Assert.Collection,但是并没有删除上面代码中的Assert.Equal(expected.Count,actual.Count)语句.
感谢您的答案提前.
解决方法 来自xunit.net的Brad Wilson在 Github Issue号告诉我,应该使用liNQ的OrderBy运算符,然后使用Assert.Equal来验证两个集合是否包含相同的项目而不关于它们的顺序.当然,您必须在相应的项目类上有一个属性,您可以首先使用它来排序(在我的情况下我真的没有).就个人而言,我通过使用FluentAssertions,解决了这个问题,该库提供了很多可以流利的风格应用的断言方法.当然,there are also a lot of methods that you can use to validate collections.
在我的问题的上下文中,我将使用如下代码:
[Fact]public voID Foo(){ var first = new[] { 1,2,3 }; var second = new[] { 3,1 }; first.Should().BeEquivalentTo(second);} 此测试通过,因为BeEquivalentTo调用忽略项的顺序.
Shouldly也是一个很好的选择,如果你不想与FluentAssertions一起去.
总结以上是内存溢出为你收集整理的c# – 在xunit.net中有一个简单的方法来比较两个集合,而不考虑项目的顺序?全部内容,希望文章能够帮你解决c# – 在xunit.net中有一个简单的方法来比较两个集合,而不考虑项目的顺序?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)