[rspec-users] Cucumber: Group Given, Whens and Thens

Nicolás Sanguinetti godfoca at gmail.com
Mon Nov 24 12:02:00 EST 2008

On Thu, Nov 20, 2008 at 8:05 PM, David Chelimsky <dchelimsky at gmail.com> wrote:
> On Thu, Nov 20, 2008 at 10:40 AM, Andrew Premdas <apremdas at gmail.com> wrote:
>> Also occasionally you may want to use a when as a given. Cucumber
>> doesn't actually use the given|when|then|and to differentiate steps,
>> just the regex that comes after them. Sometimes I feel that it makes
>> sense as in the following examples
>>    When I log in as Fred
>>    ....
>>    Given I log in as Fred
>> When I'm writing the login feature I'd use the first statement, but
>> when I'm testing foo as a logged in user I'd use the second. Both
>> would use the same step.
>> Now I realise that this counters the excellent advice given on the
>> step-organisation page of the wiki. However I think in this case even
>> though the step is definitely a when it should be allowed to be used
>> as a Given in some stories as its definitely a setup step rather than
>> an interaction step in certain|most contexts
> I actually prefer to keep these separate:
>  Given I am logged in as Fred
>  When I log in as Fred
> And then have each call out to a helper method named login_as(name).
> This reduces the duplication in the code in the steps, but allows me
> to be consistent about the distinctions I make between how Givens and
> Thens are expressed.

I also keep both steps separate, but just call the When from inside the Given

Given "a user logged in as $name" do |name|
  When "I log in as #{name}"

Given "an anonymous user" do
  When "I log out"

When "I log in as $name" do |name|
  post '/sessions', :login => name, :password => 'monkey'

When "I log out" do
  get "/logout"


More information about the rspec-users mailing list