In specs2, what is the correct way to express a subtest pattern that only runs if its โparentโ test returned a result without exception?
I have a maybeGiveMeAThing function, and it can either return Thing or exclude throws.
The call is as follows:
val thing: Thing = maybeGiveMeAThing("foo", "bar" "baz" )
I want to verify that with a specific set of inputs, maybeGiveMeAThing successfully returns Thing without throwing an exception, and using the returned Thing, run additional tests to make sure that this is the correct Thing returned for the parameters given to maybeGiveMeAThing .
As I have currently configured tests, if the maybeGiveMeAThing call throws an exception, the entire test suite is interrupted. This will be the logic that I prefer:
- If a
Thing was successfully returned, go to the set of subtests that analyze the contents of the thing - If
maybeGiveMeAThing throws an exception (any exception), skip the subtests that analyze the subject, but continue with the rest of the tests.
My existing test code looks something like this:
// ... "with good parameters" in { var thing: Thing = null "return a Thing without throwing an exception" in { thing = maybeGiveMeAThing("some", "good", "parameters", "etc.") } should not(throwA[Exception]) "the Thing returned should contain a proper Foo" in { thing.foo mustBe "bar" } //... etc ... } // ... }
... although this is similar to how to do it right. What will be the right way?
(I would like to avoid using var if I can help it.)
source share