Why exceptions in MethodContext.Build are not caught?
See original GitHub issueWe noticed a behavior while working at our NSpec VS adapter, with a spec class similar to the following:
class SomeSpec: nspec
{
// ...
void Given_a_method_context()
{
// while building some data used in this context, an exception is thrown
InputData badInput = null;
FixtureData expected = new FixtureData(badInput); // <-- Boom
FixtureData actual = null;
before = () => { ... };
act = () => { actual = ... };
it["Should test actual Vs expected"] = () => actual.Should().Be(expected);
}
}
we noticed that, for interested assembly/binary, the whole discovery phase in our adapter fails (gracefully) with 0 found results for the whole binary, even if only that context in that class exposed a problem.
The trigger seems to be an exception thrown when running the main body of method context (note: context body, not the body of before
, act
, … hooks). In such a scenario:
NSpec.Domain.MethodContext.Build()
catches exception only to log a message to console which, incidentally, is not visible when running from within VS test adapter. Then it just throws exception again- As there’s no other catch, the whole call chain up to
NSpec.Domain.ContextCollection.Build()
fails as well - At this point, our adapter had invoked such code remotely so to run on test assembly. Uncaught exception causes a
TargetInvocationException
in adapter code, which gets caught and logged in VS test runner output window. But this also causes the whole test discovery on that assembly to fail.
We were thinking whether there’s a way to only make fail a specific context (the one that threw) and its descendants. Any idea? Thanks
Issue Analytics
- State:
- Created 7 years ago
- Comments:13
Top Results From Across the Web
Java exception not caught?
S.D. Since throw new Exception("2"); is thrown from catch block and not try , it won't be caught again. See 14.20. 2.
Read more >Exception propagation: When should I catch exceptions?
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has...
Read more >Specifying the Exceptions Thrown by a Method
If the writeList method doesn't catch the checked exceptions that can occur within it, the writeList method must specify that it can throw...
Read more >Exceptions - Manual
If an exception is thrown and its current function scope has no catch block, the exception will "bubble up" the call stack to...
Read more >Entity Framework Core in Action, Second Edition
If not, the ChangeCheck value won't be returned, and EF Core will know that a concurrent ... The timestamp approach catches any update...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s how it could look like: https://github.com/mattflo/NSpec/tree/feat/bare-code-failing-spec
Totally right. I was thinking about “bare” code run within a sub-context
context[...] = () => { ... }
that is not a hook, e.g.:Does that makes sense?