Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Asserting two objects are equal in unit tests

Tags:

Either using Nunit or Microsoft.VisualStudio.TestTools.UnitTesting. Right now my assertion fails.

    [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);     } 
like image 726
PercivalMcGullicuddy Avatar asked May 08 '14 01:05

PercivalMcGullicuddy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

You've got two different Board instances, so your call to Assert.AreEqual will fail. Even if their entire contents appear to be the same, you're comparing references, not the underlying values.

You have to specify what makes two Board instances equal.

You can do it in your test:

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... 

Or you can do it in your classes: (note I wrote this on-the-fly - you'll want to adjust this)

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     } } 
like image 86
Grant Winney Avatar answered Sep 22 '22 16:09

Grant Winney