11

我需要为下一个函数编写一个单元测试,我看到我可以使用 [ExpectedException]

这是要测试的功能。

public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null)
        where T:struct
    {
        var enumType = typeof (T);

        if (!enumType.IsEnum)
        {
            throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName));
        } 
        else if (!Enum.IsDefined(enumType, enumValue))
        {
            throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? ""));
        }

        return enumValue;
    }

这里将使用代码来测试应该抛出的异常

    [TestMethod] 
    [ExpectedException(ArgumentOutOfRangeException(ArgumentException), "message")]
    public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
    {
        // PREPARE
        // EXECUTE
        // ASSERT
    }

我也不知道必须为异常做出断言。

4

5 回答 5

26

ExpectedException只是断言测试方法将抛出指定类型的异常:

[TestMethod] 
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
    // PREPARE
    // EXECUTE
    // NO ASSERT!!
}

如果你想断言其他异常参数,那么你应该try..catch在你的测试方法中使用:

[TestMethod]     
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
    // PREPARE

    try
    {
       // EXECUTE
       Assert.Fail()
    }
    catch(Exception exception)
    {        
        // ASSERT EXCEPTION DETAILS
    }
}

您可以编写自己的方法来断言抛出异常,以避免一遍又一遍地重复相同的测试代码:

public TException AssertCatch<TException>(Action action)
    where TException : Exception
{
    try
    {
        action();
    }
    catch (TException exception)
    {
        return exception;
    }

    throw new AssertFailedException("Expected exception of type " + 
                                    typeof(TException) + " was not thrown");
}

用法:

var exception = AssertCatch<ArgumentOutOfRangeException>(() => /* EXECUTE */);
Assert.AreEqual("foo", exception.Message);
于 2013-12-10T12:09:52.383 回答
3

断言异常是抛出正确的异常消息:

var ex = Assert.Throws<Exception>(() => _foo.DoSomething(a, b, c));
Assert.That(ex.Message, Is.EqualTo("Your exception message"));
于 2014-11-16T13:10:05.530 回答
2

您必须以ExpectedException不同的方式使用:

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void MyTestSomething() 

然后对您的测试进行编码,以便引发预期的异常。

于 2013-12-10T12:10:40.607 回答
1

虽然ExpectedException不能按原样使用来验证异常的消息,但您可以通过继承来实现自己的异常验证逻辑ExpectedExceptionBaseAttribute

通过实现您自己的预期异常验证。您可以指定 ExpectedExceptionAttribute 类的内置方法无法处理的附加信息和要求,例如:

  • 验证异常的状态。
  • 期待不止一种类型的异常。
  • 抛出错误类型的异常时显示自定义消息。
  • 控制阴性测试的结果。

在您的情况下,它可能看起来像这样:

public sealed class ExpectedExceptionMessageAttribute<T> : ExpectedExceptionBaseAttribute
{
    readonly string _expectedMessage;
    public ExpectedExceptionMessageAttribute(string expectedMessage)
    {
        _expectedMessage = expectedMessage;
    }

    protected override void Verify(System.Exception exception)
    {
        // Handle assertion exceptions from assertion failures in the test method
        base.RethrowIfAssertException(exception);

        Assert.IsInstanceOfType(exception, typeof(T), "wrong exception type");
        Assert.AreEqual(_expectedMessage, exception.Message, "wrong exception message");
    }
}

话虽如此,我仍然倾向于使用直接try方法catch,尽管它更具体地说明了异常预计会在哪里引发:

public static void Throws<T>(Action action, Predicate<T> predicate = null) 
                    where T : Exception
{
    try
    {
        action();
    }
    catch (T e)
    {
        if (predicate == null || predicate(e))
        {
            return;
        }

        Assert.Fail($"Exception of type {typeof(T)} thrown as expected, but the provided predicate rejected it: {e}");
    }
    catch (Exception e)
    {
        Assert.Fail($"Expected exception of type {typeof(T)} but a different exception was thrown: {e}");
    }

    Assert.Fail($"No exception thrown, expected {typeof(T)}");
}
于 2017-07-23T23:36:55.497 回答
0

如果您使用 ExpectedException 属性,则不需要断言,实际上您的代码不应该能够到达断言。

看:http: //msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

如果你想确保抛出异常,你应该在应该抛出异常的操作之后放置一个 Assert.Fail() ,在这种情况下,如果没有抛出异常,测试将失败。

于 2013-12-10T12:11:03.443 回答