Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单元测试中断言(如果有)重复项?_C#_Linq_List_Unit Testing_Stub - Fatal编程技术网

C# 如何在单元测试中断言(如果有)重复项?

C# 如何在单元测试中断言(如果有)重复项?,c#,linq,list,unit-testing,stub,C#,Linq,List,Unit Testing,Stub,我知道 Assert.IsFalse(postsPageOne.Intersect(postsPageTwo).Any()); 您可以与对象进行比较以查找任何重复项 但在我的方法中使用列表后,我想检查列表是否包含重复项。以下是测试代码: ///ARRANGE /// var toShuffle = new List<int>(){ 1001, 1002, 1003, 1004, 1005, 1006, 1007, 10

我知道

Assert.IsFalse(postsPageOne.Intersect(postsPageTwo).Any());
您可以与对象进行比较以查找任何重复项

但在我的方法中使用列表后,我想检查列表是否包含重复项。以下是测试代码:

///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!");
///ARRANGE
///
var toShuffle=新列表(){
1001,
1002,
1003,
1004,
1005,
1006,
1007,
1008,
1009,
1010
};
///表演
///
toShuffle=Shared_Components.SharedComponents.Shuffle(toShuffle,10);
///断言
///
IsTrue(toShuffle.Count==10,“元素数不正确!”);

Assert.IsTrue(toShuffle.All(a=>a>=1001&&a查看不同值的数量(
toShuffle.distinct().Count())
并验证是否与初始数量相同


我还建议您使用正确的断言方法,而不是到处使用
Assert.IsTrue()

查看不同值的数量(
toShuffle.distinct().Count())
,并验证是否与初始数量相同

我还建议您使用正确的断言方法,而不是到处使用
Assert.IsTrue()

使用(我强烈建议),您可以:

toShuffle.Should().OnlyHaveUniqueItems();
但是,我实际上会像这样重写你的测试:

//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();
//排列
var original=新列表{100110021003004100510061007100810091010};
//表演
var shuffled=Shared_Components.SharedComponents.Shuffle(原始,10);
//断言
shuffled.Should().beequivalento(原件)
.和.notbeascindinorder();
现在,测试的目的更容易理解。

使用(我强烈建议),您可以执行以下操作:

toShuffle.Should().OnlyHaveUniqueItems();
但是,我实际上会像这样重写你的测试:

//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();
//排列
var original=新列表{100110021003004100510061007100810091010};
//表演
var shuffled=Shared_Components.SharedComponents.Shuffle(原始,10);
//断言
shuffled.Should().beequivalento(原件)
.和.notbeascindinorder();

现在,测试的目的更容易理解。

可能是我的解释不够好和/或示例不合适

我是这样解决的:

///ARRANGE
///
var toShuffle = new List<int>(){1001, 1002,1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010};
var expected = new List<int>() { 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010 };

///ACT
///
toShuffle = Shared_Components.SharedComponents.Shuffle(toShuffle, 10);

///ASSERT
///
Assert.AreEqual(10, toShuffle.Count, "Number of Elements wrong!");
Assert.IsTrue(toShuffle.All(a => a >= 1001 && a <= 1010), "Elements out of range!");

//to check if there are any duplicates
toShuffle.Sort();
CollectionAssert.AreEqual(expected, toShuffle, "Duplicates found!");
///ARRANGE
///
var toShuffle=new List(){10011003、1004、1005、1006、1007、1008、1009、1010};
var expected=new List(){1001、1002、1003、1004、1005、1006、1007、1008、1009、1010};
///表演
///
toShuffle=Shared_Components.SharedComponents.Shuffle(toShuffle,10);
///断言
///
AreEqual(10,toShuffle.Count,“元素数错误!”);

Assert.IsTrue(toShuffle.All)(a=>a>=1001&&a可能是我的解释不够好和/或示例不合适

我是这样解决的:

///ARRANGE
///
var toShuffle = new List<int>(){1001, 1002,1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010};
var expected = new List<int>() { 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010 };

///ACT
///
toShuffle = Shared_Components.SharedComponents.Shuffle(toShuffle, 10);

///ASSERT
///
Assert.AreEqual(10, toShuffle.Count, "Number of Elements wrong!");
Assert.IsTrue(toShuffle.All(a => a >= 1001 && a <= 1010), "Elements out of range!");

//to check if there are any duplicates
toShuffle.Sort();
CollectionAssert.AreEqual(expected, toShuffle, "Duplicates found!");
///ARRANGE
///
var toShuffle=new List(){10011003、1004、1005、1006、1007、1008、1009、1010};
var expected=new List(){1001、1002、1003、1004、1005、1006、1007、1008、1009、1010};
///表演
///
toShuffle=Shared_Components.SharedComponents.Shuffle(toShuffle,10);
///断言
///
AreEqual(10,toShuffle.Count,“元素数错误!”);

Assert.IsTrue(toShuffle.All(a=>a>=1001&&a)您真的应该使用Assert.AreEqual(10,toShuffle.Count,“元素数不正确!”),而不是Assert.IsTrue()。这将使失败的测试报告更有用。@Grant Winney:Shuffle()方法更改[索引]每个数字的位置。您确实应该使用Assert.AreEqual(10,toShuffle.Count,“元素数不正确!”),而不是Assert.IsTrue()。这将使失败的测试报告更有用。@Grant Winney:Shuffle()方法更改每个数字的[index]位置。