CollectionAssert.AreEquivalent失败...无法找出原因 [英] CollectionAssert.AreEquivalent failing... can't figure out why

查看:112
本文介绍了CollectionAssert.AreEquivalent失败...无法找出原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了单元测试的注入接口.有问题的方法正在工作,但是我正在尝试编写一个单元测试,以确认返回的样本数据是完整且准确的.我的测试对我来说看起来是正确的,甚至结果看起来都一样,但是测试失败,并显示"CollectionAssert.AreEquivalent失败.预期的集合包含1次出现.实际的集合包含0次出现."

I've got an injected interface I'm unit testing. The method in question is working, but I'm trying to write a unit test that confirms the returned sample data is complete and accurate. My test looks correct to me, and even the results look identical, yet the test fails with "CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of . The actual collection contains 0 occurrence(s)."

[TestMethod]
    public void Should_Get_All_Amenities()
    {
        var amenitiesRep = _ninjectKernel.Get<IAmenityRepository>();

        var amenities = amenitiesRep.GetAmenities();

        var expected = new List<Amenity>
        {
            new Amenity() {Id = 1, Name = "Pool", Category = "resort"},
            new Amenity() {Id = 2, Name = "Hottub", Category = "resort"},
            new Amenity() {Id = 3, Name = "Steamroom", Category = "unit"}
        };

        Assert.IsNotNull(amenities);
        Assert.IsTrue(amenities.Count() == 3);
        CollectionAssert.AreEquivalent(expected, amenities);
    }

(我的TestRepository中的相关代码)

(Relevant code from my TestRepository)

        var amenities = new List<Amenity>
        {
            new Amenity() {Id = 1, Name = "Pool", Category = "resort"},
            new Amenity() {Id = 2, Name = "Hottub", Category = "resort"},
            new Amenity() {Id = 3, Name = "Steamroom", Category = "unit"}
        };

        var mockAmenitiesRep = new Mock<IAmenityRepository>();
        mockAmenitiesRep.Setup(_m => _m.GetAmenities()).Returns(amenities);
        Kernel.Bind<IAmenityRepository>().ToConstant(mockAmenitiesRep.Object);

我可以在所有的数据在CollectionAssert正确填充确认,每场出现匹配1比1,相同数量的对象,相同的对象的类型,所以我刚失去为什么测试失败.

I can confirm at all the data is populated correctly at the CollectionAssert, every field appears to match 1 to 1, same number of objects, same object types, so I'm just lost why the test is failing.

(代码失败的那一行是CollectionAssert)

( Line the code fails on is the CollectionAssert)

推荐答案

这是因为Amenity是引用类型,所以CollectionAssert.AreEquivalent通过引用地址检查是否相等.由于预期集合中的项目与您从GetAmenities()方法获得的对象不同,因此它返回false.您必须在Amenity类中重写相等比较器.

It's because Amenity is a reference type so CollectionAssert.AreEquivalent checks equality by referenced addresses. Since the items in the expected collection are not the same objects you get from GetAmenities() method, it returns false. You have to override equality comparers in Amenity class.

public override bool Equals(object obj)
{
    var other = obj as Amenity;
    if(other == null)
    {
        return false;
    }

    return Id = other.Id && Name == other.Name && Category == other.Category;
}

public override int GetHashCode()
{
    return Id.GetHashCode(); //assumes Id is immutable
}

更新:

请记住,这种方法不是很好,因为它会导致平等污染.亚历山大·史蒂芬努克(Alexander Stepaniuk)在评论中发布了一个更好的选择.

Keep in mind this approach is not very good because it leads to Equality Pollution. Alexander Stepaniuk posted a better alternative in comments.

这篇关于CollectionAssert.AreEquivalent失败...无法找出原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆