0

during unit testing have some problem using Assert.Equels(expected, actual). I have the next message

Expected: MyClass { Id = 1, Name = "Vasia Vasiliew", Value = 10 }

Actual: MyClass { Id = 1, Name = "Vasia Vasiliew", Value = 10 }

but the Assert.Equal() Failure

Assert.True(actual.equels(expected)) is failure too

How can I fix it?

Alex King
  • 1
  • 1
  • 1
    Perhaps the instances are not the same. Try Changing your `class` to `record` this will force it to compare properties – AMunim Jun 24 '22 at 16:40
  • 1
    What does your `MyClass` look like? By default C# implements class equals by checking whether the references are equal, so you probably need to manually override the `bool Equal(Object other)` member – ph3rin Jun 24 '22 at 16:40
  • Note that the *override* is `bool Equals(object obj)`. You might *also* want to implement `IEquatable` with a signature of `bool Equals(MyClass other)`. – Jon Skeet Jun 24 '22 at 16:41
  • You need to define "equality" for your class - by default, classes use _instance_ equality. See [this question](https://stackoverflow.com/questions/2363143) for an example – D Stanley Jun 24 '22 at 16:41
  • As the other comments say, Assert.Equals check the reference on classes. Either use a record, implement the equality function or use Fluent Assertions which actually traverses the object graph – scottdavidwalker Jun 25 '22 at 09:14

0 Answers0