Exceptions

922 views
Skip to first unread message

HL

unread,
Jan 14, 2010, 7:32:49 AM1/14/10
to SpecFlow, hla...@maninvestments.com
I'm pretty new to this but I've got what I hope isn't a stupid
question:

I've been messing about with the bowling samples

If I write a scenario such as:

Scenario: Too many throws
Given a completed game
When i roll anything
Then an eror should be raised

How do I test in the "then" for an exception thrown in the "When"

Xerxes Battiwalla

unread,
Jan 14, 2010, 4:06:02 PM1/14/10
to spec...@googlegroups.com
The immediate answer to your question is to catch the exception in the When block, and store the exception in a field. You can then make your assertions on the field in the Then block.

But it begs to ask the question why you're testing for an exception in the first place. Your application should be the one catching the exception and displaying a more friendly message to the user, and your scenario should be testing for that instead:

Scenario: Too many throws

 Given a completed game
 When i roll anything
 Then the Game Over message is shown.

The important difference here is that you're not testing for a specific implementation of HOW the UI is notified that the game is over. In your case you're using an exception to signify that the game is over, but you could just as easily use a return code or something altogether different and the scenario is still valid, where previously you'd tied your scenario down tightly to the details of your implementation.

-xerx
Reply all
Reply to author
Forward
0 new messages