NUnit无法与Assert.AreEqual配合使用 [英] NUnit doesn't work well with Assert.AreEqual

查看:57
本文介绍了NUnit无法与Assert.AreEqual配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试(尤其是NUit)的新手.我只是输入本书中涉及Java和JUnit的一些示例.但是我改用C#.

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.

问题是:我有一个带有重写方法的类,例如 Equals() GetHashCode(),但是当我尝试比较两个对象时此类带有 Assert.AreEqual()的类未调用我的代码,因此出现异常.

The problem is: I've got a class with overriden methods such as Equals() and GetHashCode(), but when I am trying to compare two objects of this class with Assert.AreEqual() my code is not called, so I get an exception.

Assert.True(MyClass.Equals(MyClass2))效果很好.但是我不想使用这种结构代替 Assert.AreEqual().问题出在哪里?

Assert.True(MyClass.Equals(MyClass2)) does work well. But I don't want to use this construction instead of Assert.AreEqual(). Where can the problem be?

这是课程:

public class Money
{
    public int amount;
    protected string currency;

    public Money(int amount, string currency)
    {
        this.amount = amount;
        this.currency = currency;
    }

    public new bool Equals(object obj)
    {
        if (obj == null)
            return false;

        Money money = (Money)obj;
        return (amount == money.amount)
                && (Currency().Equals(money.Currency()));
    }

    public new int GetHashCode()
    {
        return (string.Format("{0}{1}", amount, currency)).GetHashCode();
    }

    public static Money Dollar(int amount)
    {
        return new Money(amount, "USD");
    }
    public static Money Franc(int amount)
    {
        return new Money(amount, "CHF");
    }

    public Money Times(int multiplier)
    {
        return new Money(amount * multiplier, currency);
    }

    public string Currency()
    {
        return currency;
    }
}

测试方法本身:

[TestFixture]
public class DollarTest
{
    [Test]
    public void TestMultiplication()
    {
        Money five = Money.Dollar(5);
        Assert.True(Money.Dollar(10).Equals(five.Times(2)));  // ok
        Assert.AreEqual(Money.Dollar(10), five.Times(2));     // fails
    }
}

推荐答案

问题是您正在隐藏等于,而不是覆盖.做得好-您的单元测试发现了一个错误:)

The problem is you're hiding Equals, not overriding it. Well done - your unit test has found a bug :)

您的代码应为:

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 更改为属性,而不是方法
  • 添加一个 Amount 属性
  • 可能将 amount 的类型更改为 decimal 而不是 int
  • 将字段设为私有和只读
  • 密封课程
  • 为==和!=添加运算符重载
  • 可能添加*运算符重载以执行与 Times
  • 相同的操作
  • 计算散列时避免字符串格式设置(有数十个答案显示了更好的散列实现)
  • 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)

我刚刚重读了您使用的是书中的示例.这本书真的隐藏而不是覆盖 Equals 方法吗?我建议您买一本新书,如果可以的话(除非这是一个故意的例子,说明什么时候使用隐藏隐藏是错误!)...是哪本书?

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?

这篇关于NUnit无法与Assert.AreEqual配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆