12

Is there an assertion built into Nunit that checks all properties between 2 objects are the same, without me having to override Equals?

I'm currently using reflection to Assert each individual property for a pair of objects.

Castrohenge
  • 8,525
  • 5
  • 39
  • 66

3 Answers3

3

I don't believe there is.

Assert.AreEqual compares non-numeric types by Equals.
Assert.AreSame checks if they refer to the same object

AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
1

You can write framework agnostic asserts using a library called Should. It also has a very nice fluent syntax which can be used if you like fluent interfaces. I had a blog post related to the same.

http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html

You can two objects and there properties with ShouldBeEquivalentTo

dto.ShouldBeEquivalentTo(customer);
dannash918
  • 434
  • 6
  • 14
Nilesh Gule
  • 1,511
  • 12
  • 13
0

https://github.com/kbilsted/StatePrinter has been written specifically to dump object graphs to string representation with the aim of writing easy unit tests.

  • It comes witg Assert methods that output a properly escaped string easy copy-paste into the test to correct it.
  • It allows unittest to be automatically re-written
  • It integrates with all unit testing frameworks
  • Unlike JSON serialization, circular references are supported
  • You can easily filter, so only parts of types are dumped

Given

class A
{
  public DateTime X;
  public DateTime Y { get; set; }
  public string Name;
}

You can in a type safe manner, and using auto-completion of visual studio include or exclude fields.

  var printer = new Stateprinter();
  printer.Configuration.Projectionharvester().Exclude<A>(x => x.X, x => x.Y);

  var sut = new A { X = DateTime.Now, Name = "Charly" };

  var expected = @"new A(){ Name = ""Charly""}";
  printer.Assert.PrintIsSame(expected, sut);
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114