I\'m new to unit testing and NUit in particular. I\'m just typing some examples from the book which refers to Java and JUnit. But I\'m using C# instead.
The problem
The problem is you're hiding Equals, not overriding it. Well done - your unit test has found a bug :)
Your code should be:
public override bool Equals(object obj)
{
Money money = obj as Money;
if (money == null)
return false;
return (amount == money.amount && currency == money.currency);
}
(This will prevent it from throwing an exception if you give it the wrong type, too.)
I've made the string equality test simpler too - operator overloading can be very helpful :)
By the way, you almost certainly want to:
Currency
to be a property, not a methodAmount
propertyamount
to be decimal
instead of int
Times
EDIT: I've just reread that you're using an example from a book. Does the book really hide instead of overriding the Equals
method? I suggest you get a new book, if so (unless it's being a deliberate example of when it's wrong to use hiding!)... which book is it?