如何使用Assert。抛出以断言异常的类型和实际的消息措辞?
就像这样:
Assert.Throws<Exception>(
()=>user.MakeUserActive()).WithMessage("Actual exception message")
我正在测试的方法抛出了多个具有不同消息的相同类型的消息,我需要一种方法来测试是否根据上下文抛出了正确的消息。
如何使用Assert。抛出以断言异常的类型和实际的消息措辞?
就像这样:
Assert.Throws<Exception>(
()=>user.MakeUserActive()).WithMessage("Actual exception message")
我正在测试的方法抛出了多个具有不同消息的相同类型的消息,我需要一种方法来测试是否根据上下文抛出了正确的消息。
当前回答
Assert.That(myTestDelegate, Throws.ArgumentException
.With.Property("Message").EqualTo("your argument is invalid."));
其他回答
您现在可以使用ExpectedException属性,例如:
[Test]
[ExpectedException(typeof(InvalidOperationException),
ExpectedMessage="You can't do that!"]
public void MethodA_WithNull_ThrowsInvalidOperationException()
{
MethodA(null);
}
由于我被一些新的NUnit模式的冗长所困扰,我使用这样的方法来创建对我个人来说更清晰的代码:
public void AssertBusinessRuleException(TestDelegate code, string expectedMessage)
{
var ex = Assert.Throws<BusinessRuleException>(code);
Assert.AreEqual(ex.Message, expectedMessage);
}
public void AssertException<T>(TestDelegate code, string expectedMessage) where T:Exception
{
var ex = Assert.Throws<T>(code);
Assert.AreEqual(ex.Message, expectedMessage);
}
用法是:
AssertBusinessRuleException(() => user.MakeUserActive(), "Actual exception message");
断言例外:
Junit 5:
@Test
public void whenExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(NumberFormatException.class, () -> {
Integer.parseInt("1a");
});
String expectedMessage = "For input string";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
在Junit 4中:
@Test(expected = NullPointerException.class)
public void whenExceptionThrown_thenExpectationSatisfied() {
String test = null;
test.length();
}
要扩展persistent的答案,并提供更多的NUnit功能,你可以这样做:
public bool AssertThrows<TException>(
Action action,
Func<TException, bool> exceptionCondition = null)
where TException : Exception
{
try
{
action();
}
catch (TException ex)
{
if (exceptionCondition != null)
{
return exceptionCondition(ex);
}
return true;
}
catch
{
return false;
}
return false;
}
例子:
// No exception thrown - test fails.
Assert.IsTrue(
AssertThrows<InvalidOperationException>(
() => {}));
// Wrong exception thrown - test fails.
Assert.IsTrue(
AssertThrows<InvalidOperationException>(
() => { throw new ApplicationException(); }));
// Correct exception thrown - test passes.
Assert.IsTrue(
AssertThrows<InvalidOperationException>(
() => { throw new InvalidOperationException(); }));
// Correct exception thrown, but wrong message - test fails.
Assert.IsTrue(
AssertThrows<InvalidOperationException>(
() => { throw new InvalidOperationException("ABCD"); },
ex => ex.Message == "1234"));
// Correct exception thrown, with correct message - test passes.
Assert.IsTrue(
AssertThrows<InvalidOperationException>(
() => { throw new InvalidOperationException("1234"); },
ex => ex.Message == "1234"));
一个真正有效的解决方案:
public void Test() {
throw new MyCustomException("You can't do that!");
}
[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
var exception = Assert.ThrowsException<MyCustomException>(
() => Test(),
"This should have thrown!");
Assert.AreEqual("You can't do that!", exception.Message);
}
这可以使用Microsoft.VisualStudio.TestTools.UnitTesting;。