Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get SpecFlow to expect an exception?

I'm using SpecFlow, and I'd like to write a scenario such as the following:

Scenario: Pressing add with an empty stack throws an exception     Given I have entered nothing into the calculator     When I press add     Then it should throw an exception 

It's calculator.Add() that's going to throw an exception, so how do I handle this in the method marked [Then]?

like image 513
Roger Lipscombe Avatar asked May 21 '10 08:05

Roger Lipscombe


People also ask

How do you skip tests on SpecFlow?

Ignored Tests Just like with normal unit tests, you can also ignore SpecFlow tests. To do so, tag the feature or scenario with the @ignore tag.

Which test frameworks work with a SpecFlow project?

Apart from the NUnit test framework, SpecFlow supports other popular test frameworks like MSTest v2, xUnit 2, etc.


1 Answers

Great question. I am neither a bdd or specflow expert, however, my first bit of advice would be to take a step back and assess your scenario.

Do you really want to use the terms "throw" and "exception" in this spec? Keep in mind the idea with bdd is to use a ubiquitous language with the business. Ideally, they should be able to read these scenarios and interpret them.

Consider changing your "then" phrase to include something like this:

Scenario: Pressing add with an empty stack displays an error     Given I have entered nothing into the calculator     When I press add     Then the user is presented with an error message 

The exception is still thrown in the background but the end result is a simple error message.

Scott Bellware touches this concept in this Herding Code podcast: http://herdingcode.com/?p=176

like image 182
Scott Coates Avatar answered Sep 28 '22 05:09

Scott Coates