c# - Is there a more appropriate to test if the constructor throws an exception? -
normally test, if exception gets thrown in method, follows. use fluentassertions:
[fact] public void exception_gets_thrown() { // arrange var foo = new foo("validargument"); // act/assert foo.invoking(f => f.bar(null)) // null invalid argument .shouldthrow<argumentnullexception>(); } but how test, if exception gets thrown in constructor? did this, there maybe more appropriate way via fluentassertions?
[fact] public void constructor_throws_exception() { // arrange action = () => new foo(null); // null invalid argument // act/assert a.shouldthrow<argumentnullexception>(); }
that's how you're supposed test exceptions , that's shouldthrow<t>() , shouldnotthrow<t>() designed in first place. in fact, invoking() approach might marked obsolete in next big version (2.0.0).
Comments
Post a Comment