简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM