In my 'act' I want to capture an exception so that I can do multiple tests on the exception data. Examples on the web show how to capture and compare the type/message within a test (or 'It' block) but not how to capture the exception as an 'act' in itself.
I am currently just doing a try/catch within the 'act' body and storing the exception within the context for later testing in the 'It' block. There I can perform a number of different fluent assertions on the data. Is this the best approach?
Actually, there is indeed a better way to do this:
void describe_some_exceptional_behavior()
{
context["when throwing an exception"] = () =>
{
act = () => throw new InvalidOperationException();
it["should raise the exception"] = expect<InvalidOperationException>();
};
}
Note: you assign the result of expect
directly to it
. This tripped me up the first time.
See the nspec specs for more examples.
我认为除了手动 try catch 块来存储异常并稍后在 it 块中检查它之外,目前没有另一种方法可以实现这一点。
If you're willing to use an open-source framework, you could use Fluent Assertions and do this:
Action act = () => subject.Foo2("Hello");
act.ShouldThrow() .WithInnerException() .WithInnerMessage("whatever");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.