Miguel Moura
Miguel Moura

Reputation: 39524

Test equality of two objects on testing

I have the following class:

public class OrderRule {
  public OrderDirection Direction { get; }
  public String Property { get; }
}

I created an Unit Test using XUnit as follows:

public void TryParse_ParseAscendingOrderRule() {

  OrderRule expect = new OrderRule("name", OrderDirection.Ascending);

  OrderRule result = factory.GetOrderRule("type1");

  Assert.Equal(result, expect);

}

I know expect and result have the same Direction and Property values but I still get False on my test ... I suppose this is because they are not the same instance ...

Do I really need to compare the Direction and Property as follows?

  Assert.True(result.Property == expect.Property && expect.Property == expect.Property );

This can become really long when the objects have many properties ...

Or is there a better way to do this?

Upvotes: 0

Views: 181

Answers (1)

DAXaholic
DAXaholic

Reputation: 35418

If it is not necessary for OrderRule to be a class then make it a struct which by default implements value equality. There is also a whole MSDN page about value equality which might help you.

Upvotes: 1

Related Questions