This project is read-only.

Using ShouldEqual() to compare complex object

Jan 7, 2014 at 8:02 AM
Hi All,
I'm a bit new to NBehave and unit-testing as a whole so please excuse my question if it feels a bit silly.

I've been using NBehaves various extension methods to compare simple properties like string and int types, but when I try to compare a complex object it always fails even when the objects being compared are exactly same.

Can someone guide me to some docs or blogs or jolt down some steps, which explain how to implement the functionality.

example of class whose objects i'm trying to compare is :

public class User
public int UserId { get; set; }
public string UserName { get; set; }
public bool IsBlocked { get; set; }
Jan 8, 2014 at 9:59 PM
I don't think a test framework can make the assumption that 2 objects are equal by comparing their properties. It can vary between different types in your domain, ie entites and value objects in DDD terms.
If you want it to be a value object you should write tests for that, for example.
public void Two_instances_of_User_should_be_equal_if_all_properties_are_equal()
  var u1 = new User { UserId = 1; UserName = "A"; IsBlocked = false };
  var u2 = new User { UserId = 1; UserName = "A"; IsBlocked = false };
And then make it pass by adding code to the User class (this may be helpful).