如何测试我的C#构造函数在nUnit 3中引发异常?

时间:2018-07-26 10:02:06

标签: c# unit-testing nunit

nUnit似乎已删除nUnit 3中的ExpectedException属性。所以我的问题是:当我将无效参数传递给类时,如何捕获并测试我的构造函数抛出ArgumentOutOfRangeException?

public Year(int value)
{
    if (value < 1) throw new ArgumentOutOfRangeException(nameof(value), "Cannot be less than 1.");
    if (value > 9999) throw new ArgumentOutOfRangeException(nameof(value), "Cannot be greater than 9999.");
    Value = value;
    AddMonths();
}

我的测试:

[Test]
public void ShouldNotBeAbleToCreateYear()
{
    // Arrange...
    const int yearValue = 0;

    // Act...
    var year = new Year(yearValue);

    // Assert...
    // Need to test that an ArgumentOutOfRangeException was thrown!?
}

我已经检查了nUnits文档,而答案之谜使我难以理解!

非常感谢...

0 个答案:

没有答案