【问题标题】:Unity unit tests - Check if the function call with an argument throws an exceptionUnity 单元测试 - 检查带有参数的函数调用是否引发异常
【发布时间】:2018-11-21 20:35:46
【问题描述】:

我有一个使用错误参数调用的函数,我想确保该函数抛出正确的异常和正确的消息。所以我的函数调用是这样的:

winDetector.DoMove(move)

它应该像这样抛出异常:

throw new Exception("Move is not valid.");

看起来我应该使用Assert.Throws<Exception>,但我不知道如何使用。现在到 Unity 和 C#。我该怎么做?

附: Assert.Throws<Exception> 不是正确的方法。请看下面我的回答。

【问题讨论】:

标签: c# unit-testing unity3d exception


【解决方案1】:

有一种优雅的方法可以做到这一点。应该这样做:

Assert.That(() => winDetector.DoMove(move), 
                  Throws.TypeOf<Exception>());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2020-10-14
    相关资源
    最近更新 更多