NUnit doesn't work well with Assert.AreEqual

前端 未结 4 1657
面向向阳花
面向向阳花 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)

提交回复
热议问题