我正在运行一些TestMethods来测试 JSON 序列化程序的实现,并在比较序列化 \ 反序列化之前和之后的两个 IList 对象时,但比较失败。
模型
public class BusinessModel
{
public int ID { get; set; }
public string Name { get; set; }
}
测试
IList<BusinessModel> model = new List<BusinessModel>
{
new BusinessModel { ID = 1, Name = "Test1" },
new BusinessModel { ID = 2, Name = "Test2" },
new BusinessModel { ID = 3, Name = "Test3" },
new BusinessModel { ID = 4, Name = "Test4" }
};
[TestMethod]
public async Task TestSerialiseDeserialiseIsEqual()
{
ISerialiser serialiser = new Serialiser();
IList<BusinessModel> deserialisedModel;
string serialised = string.Empty;
serialised = await serialiser.SerialiseAsync<IList<BusinessModel>>(model);
deserialisedModel = await serialiser.DeserialiseAsync<IList<BusinessModel>>(serialised);
Assert.IsTrue(model.SequenceEqual(deserialisedModel));
}
结果
我在这篇文章中尝试了许多详细的测试方法,但都失败了。
Assert.AreEqual(t1.Count(), t2.Count());
IEnumerator<Token> e1 = t1.GetEnumerator();
IEnumerator<Token> e2 = t2.GetEnumerator();
while (e1.MoveNext() && e2.MoveNext())
{
Assert.AreEqual(e1.Current, e2.Current);
}
通过集合计数项,但在第一个对象比较时失败。
Assert.IsTrue(model.SequenceEqual(deserialisedModel));
失败了Assert.IsTrue failed
CollectionAssert.AreEqual(model.ToArray(),deserialisedModel.ToArray())
失败了Collection.AreEqual failed (Element at index 0 do not match)
CollectionAssert.AreEquivalent(model.ToArray(),deserialisedModel.ToArray())
失败了{"CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <Helper.Tests.Serialisation.BusinessModel>. The actual collection contains 0 occurrence(s). "}
通过检查它们看起来相同的对象
原来的
反序列化对象
问题
比较 IList 对象以进行单元测试的最佳方法是什么?为什么我的测试失败了?