NUnit doesn't work well with Assert.AreEqual

前端 未结 4 1610
面向向阳花
面向向阳花 2021-01-17 13:29

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

相关标签:
4条回答
  • 2021-01-17 14:21

    I found it confusing that implementing the IEquatable interface, which also has an

    Equals(T other)
    

    method, posed me with the same problem as described above.

    The only reason I chose to use the IEquaytable interface above overriding the Equals method was not to have to do the type check.

    In the end I had to use the following code

    public bool Equals(CustomTag other)
    {
       return (other.Name.Trim().ToLower() == Name.Trim().ToLower());
    }
    
    public override bool Equals(object o)
    {
        if (o is CustomTag)
        {
            return Equals(o as CustomTag);
        }
        return false;
    }
    

    but then I thought, why not just leave the IEquatable interface for what it is and only override the Equals method. (less code = better)

    0 讨论(0)
  • 2021-01-17 14:23

    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:

    • Change Currency to be a property, not a method
    • Add an Amount property
    • Probably change the type of amount to be decimal instead of int
    • Make the fields private and readonly
    • Seal the class
    • Add operator overloads for == and !=
    • Possibly add a * operator overload to do the same as Times
    • Avoid string formatting when calculating the hash (there are dozens of answers showing better hash implementations)

    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?

    0 讨论(0)
  • 2021-01-17 14:28

    I suspect your problem is that you haven't overridden overload the equality == operator. Under the hood the Assert.AreEqual is probably using ==.

    See Operator Overloading Tutorial.

    Update: I ran the NUnit test through the debugger and it does indeed use the Equals method and not the == operator.

    0 讨论(0)
  • 2021-01-17 14:35

    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

    0 讨论(0)
提交回复
热议问题