C# - 在单元测试中断言两个对象相等

标签 c# .net unit-testing tdd nunit

使用 Nunit 或 Microsoft.VisualStudio.TestTools.UnitTesting。现在我的断言失败了。

    [TestMethod]
    public void GivenEmptyBoardExpectEmptyBoard()
    {
        var test = new Board();

        var input = new Board()
            {
                Rows = new List<Row>()
                    {
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                    }
            };

        var expected = new Board()
        {
            Rows = new List<Row>()
                    {
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                        new Row(){Cells = new List<int>(){0,0,0,0}},
                    }
        };

        var lifeOrchestration = new LifeOrchestration();

        var actual = lifeOrchestration.Evolve(input);

        Assert.AreEqual(expected, actual);
    }

最佳答案

您有两个不同的 Board 实例,因此您对 Assert.AreEqual 的调用将失败。即使它们的全部内容看起来相同,您也是在比较引用,而不是基础值。

您必须指定使两个 Board 实例相等的原因。

你可以在你的测试中这样做:

Assert.AreEqual(expected.Rows.Count, actual.Rows.Count);
Assert.AreEqual(expected.Rows[0].Cells[0], actual.Rows[0].Cells[0]);

// Lots more tests of equality...

或者你可以在你的类里面这样做:(注意我是即时写的 - 你会想要调整它)

public class Board
{
    public List<Row> Rows = new List<Row>();

    public override bool Equals(object obj)
    {
        var board = obj as Board;

        if (board == null)
            return false;

        if (board.Rows.Count != Rows.Count)
            return false;

        return !board.Rows.Where((t, i) => !t.Equals(Rows[i])).Any();
    }

    public override int GetHashCode()
    {
        // determine what's appropriate to return here - a unique board id may be appropriate if available
    }
}

public class Row
{
    public List<int> Cells = new List<int>(); 

    public override bool Equals(object obj)
    {
        var row = obj as Row;

        if (row == null)
            return false;

        if (row.Cells.Count != Cells.Count)
            return false;

        if (row.Cells.Except(Cells).Any())
            return false;

        return true;
    }

    public override int GetHashCode()
    {
        // determine what's appropriate to return here - a unique row id may be appropriate if available
    }
}

关于C# - 在单元测试中断言两个对象相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530982/

相关文章:

c# - .net 中请求不等待响应

javascript - 如何测试 React 组件使用 jest 渲染列表

java - 有没有办法自动化 junit bean 属性测试?

c# - 使用 where 语句的 Linq to NHibernate 包装器问题

c# - 过滤某些浏览器的 "Top Sites"功能生成的网站访问

c# - 使用 Linq to SQL 从数据库中检索图像

.net - .NET 十进制到 SQL Server 十进制的良好映射是什么?

c# - 首先在 Entity Framework 代码中查询自定义类型

c# - 从 .NET 使用 SAP Web 服务会抛出异常 ServiceModel.FaultException

c# - Noda 时间单元测试 XML 错误