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);
}
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
}
}
I used to override getHasCode and equals, but I never liked it since I don't want to change my production code for the sake of unit testing. Also it's kind of pain.
Then I turned too reflection to compare objects which was less invasive...but that's kind of lot of work (lots of corner cases)
In the end I use:
http://www.nuget.org/packages/DeepEqual/
Works great.
ExpectedObjects would help you to compare equality by property value. It supports:
- simple object: expected.ToExpectedObject().ShouldEqual(actual);
- collection: expected.ToExpectedObject().ShouldEqual(actual);
- composized object: expected.ToExpectedObject().ShouldEqual(actual);
- partial compare: expected object need design with anonymous type, and use expected.ToExpectedObject().ShouldMatch(actual)
I love ExpectedObjects because of I only need to invoke 2 API for assertion of comparing object equality:
- ShouldEqual()
- ShouldMatch() for partial comparing
I wanted a solution that didn't require adding a dependency, worked with VS unit tests, compared the field values of two objects,and told me all unequal fields. This is what I came up with. Note it could be extended to work with property values as well.
In my case, this works well for comparing the results of some file-parsing logic to ensure two technically "different" entries have fields with the same values.
public class AssertHelper
{
public static void HasEqualFieldValues<T>(T expected, T actual)
{
var failures = new List<string>();
var fields = typeof(T).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach(var field in fields)
{
var v1 = field.GetValue(expected);
var v2 = field.GetValue(actual);
if (v1 == null && v2 == null) continue;
if(!v1.Equals(v2)) failures.Add(string.Format("{0}: Expected:<{1}> Actual:<{2}>", field.Name, v1, v2));
}
if (failures.Any())
Assert.Fail("AssertHelper.HasEqualFieldValues failed. " + Environment.NewLine+ string.Join(Environment.NewLine, failures));
}
}
[TestClass]
public class AssertHelperTests
{
[TestMethod]
[ExpectedException(typeof(AssertFailedException))]
public void ShouldFailForDifferentClasses()
{
var actual = new NewPaymentEntry() { acct = "1" };
var expected = new NewPaymentEntry() { acct = "2" };
AssertHelper.HasEqualFieldValues(expected, actual);
}
}
Assert methods rely on the object's Equals and GetHashcode. You can implement that, but if this object equality is not needed outside unit tests I would instead consider comparing the individual primitive types on the object. Looks like the objects are simple enough and overriding of equals is not really warranted.
来源:https://stackoverflow.com/questions/23530982/c-sharp-asserting-two-objects-are-equal-in-unit-tests