AJ_*_*_83 0 c# linq unit-testing list stub
我知道
Assert.IsFalse(postsPageOne.Intersect(postsPageTwo).Any());
Run Code Online (Sandbox Code Playgroud)
你可以比较对象来找到任何重复.
但是我想在我的方法中使用它之后检查我的列表是否包含重复项.这是测试代码:
///ARRANGE
///
var toShuffle = new List<int>(){
1001,
1002,
1003,
1004,
1005,
1006,
1007,
1008,
1009,
1010
};
///ACT
///
toShuffle = Shared_Components.SharedComponents.Shuffle(toShuffle, 10);
///ASSERT
///
Assert.IsTrue(toShuffle.Count == 10, "Number of Elements not correct!");
Assert.IsTrue(toShuffle.All(a => a >= 1001 && a <= 1010), "Elements out of range!");
Run Code Online (Sandbox Code Playgroud)
查看不同值的数量(toShuffle.Distinct().Count())并验证是否与初始数量相同.
我还建议你使用正确的断言方法,而不是在Assert.IsTrue()任何地方使用.
使用FluentAssertions(我强烈建议),您可以执行以下操作:
toShuffle.Should().OnlyHaveUniqueItems();
Run Code Online (Sandbox Code Playgroud)
但是,实际上我会像这样重写您的测试:
//Arrange
var original = new List<int>{1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
//Act
var shuffled = Shared_Components.SharedComponents.Shuffle(original , 10);
//Assert
shuffled.Should().BeEquivalentTo(original)
.And.NotBeAscendingInOrder();
Run Code Online (Sandbox Code Playgroud)
现在,测试的目的更加容易理解。