首先 -你真的不应该从方法返回匿名类型对象!以后在任何地方都很难用这种返回值做任何事情。
我将创建名为Devided的新结构:
public struct Devided
{
public int Reminder { get; set; }
public List<int> Numbers { get; set; }
public override bool Equals(object obj)
{
if(!(obj is Devided))
return false;
var d = (Devided)obj ;
if(object.ReferenceEquals(this, d))
return true;
return this.Reminder == d.Reminder && this.Numbers.SequenceEqual(d.Numbers);
}
public override int GetHashCode()
{
return Reminder;
}
}
我已经超越 Equals
并GetHashCode
开始CollectionAssert
工作。
使用Devided
类,您可以将方法返回类型更改为泛型IList<Devided>
:
public static IList<Devided> dividedNumbersto5(IEnumerable<int> NumberOfCollection)
{
IList<Devided> reminderNumber = NumberOfCollection.ToList().GroupBy(g => g % 5).OrderBy(g => g.Key)
.Select(g => new Devided { Numbers = g.ToList(), Reminder = g.Key }).ToList();
return reminderNumber;
}
并断言测试:
[TestMethod]
public void TestMethod1()
{
IEnumerable<int> numberOfCollection = new List<int> { 5, 14, 9, 8 };
IList remindernumber = dividedNumbersto5(numberOfCollection);
IList expectedNumberCollection = new List<Devided>
{
new Devided { Numbers = new List<int>() { 5 }, Reminder = 0 },
new Devided { Numbers = new List<int>() { 8 }, Reminder = 3 },
new Devided { Numbers = new List<int>() { 14, 9 }, Reminder = 4 }
};
CollectionAssert.AreEqual(expectedNumberCollection, remindernumber);
}
Reminder
如果您愿意,您可以只检查s:
var expectedNumberCollection = new List<int> { 0, 3, 4 };
Assert.IsTrue(expectedNumberCollection.SequenceEqual(remindernumber.Select(x => x.Reminder)));