0

Let's say I've got test methods A, B, C. When I launch them all, test method B throws SQLiteException and everything is green and ok.

Assert.Throws<SQLiteException>(() => sql.Select(selectFrom + "table_name"));

But, when I'm launching ONLY test method B it throws ArgumentException BEFORE SQLiteException and test fails.

The question is: how to assert that one OR the other exception is thrown?

I'm talking about something like this

Assert.Throws<SQLiteException>(() => sql.Select(selectFrom + "table_name")).OR.Throws<ArgumentException>()
4

1 回答 1

1
try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail(); // If it gets to this line, no exception was thrown
} catch (GoodException) { }

您应该能够根据您的喜好调整这种方法,包括您想要捕获的特定异常。如果您只期望某些类型,请使用以下命令完成 catch 块:

} catch (GoodException) {
} catch (Exception) {
    // don't want this exception
    Assert.Fail();
}

记住你不能这样做

try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail();
} catch (Exception) { }

因为 Assert.Fail() 通过抛出 AssertionException 来工作。

你也可以这样做

try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail("no exception thrown");
} catch (Exception ex) {
    Assert.IsTrue(ex is SpecificExceptionType);
}
于 2013-08-20T10:37:20.020 回答