开发者

Nunit 2.5.10 - Not being able to compare two objects

I've been looking for a way to check two objects, but I find Equals, Assert,AreEqual and some other ones and I do not know which one I should use. Furthermore, I've been making my tests with the new Nunit syntax and I have a hard time finding the new syntax for comparing two objects. Here's my test and my code:

[Test]
public void CheckForDriversSelectedMoreThanOnce_ReturnsDriversSelectedMoreThanOnceAndTheirSelectedPositions()
{
  //arrange
  Prediction prediction = new Prediction();

  Driver expected = new Driver { position = 1, name = "Michael Schuma开发者_Go百科cher" };

  //act
  var actual = prediction.CheckForDriversSelectedMoreThanOnce();

  //assert
  //Assert.That(actual, Is.EqualTo(expected));
  //Assert.That(Is.Equals(actual, expected);
  Assert.AreEqual(expected, actual);
}

public Driver CheckForDriversSelectedMoreThanOnce()
{
  Driver driver = new Driver { position = 1, name = "Michael Schumacher" };
  return driver;
}

The line Assert.That(actual, Is.EqualTo(expected)); and Assert.AreEqual(expected, actual); gives me Expected: But was:

The other line Assert.That(Is.Equals(actual, expected); gives me: Expected: True But was: False


You may need to implement IEquatable within your Class, similiar to;

   public bool Equals(Type other)
        {
            return Name == other.Name && Position == other.Position;
        }

I had this problem occur when attempting to compare 2 objects in a linq statement using 'Contains' and the above resolved that.

The default implementation of instance Equals() depends on your object type. Reference types inherit System.Object’s default implementation, which is a simple object identity (ReferenceEquals()) check. By default, reference types will return true on instance Equals() if and only if they are the same object instance.

I am assuming that Assert.AreEqual(obj1, obj2) at some point does obj1.Equals(obj2), in which case they will not match.

Another option would be to Assert the actual properties, rather than matching the objects

Assert.AreEqual("Michael Schumacher", actual.Name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜