# Cucumber: Group Given, Whens and Thens

**URL:** <https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728>\
**Category:** RSpec\
**Created:** [November 20, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728 "2008-11-20T16:29:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 20, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/1 "2008-11-20T16:29:17Z")

</div>

Hi,

Do you think this is a satisfactory way to group step files related to  
one feature

e.g.

publish\_content\_givens  
publish\_content\_whens  
publish\_content\_thens

?

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 20, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/2 "2008-11-20T16:49:22Z")

</div>

Have a read of this:

> **[GitHub: Let’s build from here](https://github.com/)**
>
> GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

Basically rather than saving your steps in files named around the  
feature, it’s better to group them around the object(s) they work on.

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 20, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/3 "2008-11-20T17:41:04Z")

</div>

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

Finally Cucumber tells you which step is matching your feature so  
there is much less need to organise steps by given|when… then there  
was with the previous story runner

All best

Andrew

2008/11/20 aidy lewis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 21, 2008, 1:46am UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/4 "2008-11-21T01:46:25Z")

</div>

On 20 Nov 2008, at 22:05, David C. wrote:

> On Thu, Nov 20, 2008 at 10:40 AM, Andrew P.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> I actually prefer to keep these separate:
> 
> Given I am logged in as Fred  
> When I log in as Fred

+1, though these days I would probably have the When step matcher call  
the Given step directly.

cheers,  
Matt

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 23, 2008, 1:48am UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/5 "2008-11-23T01:48:50Z")

</div>

Interesting … so this establishes a convention to use in features  
that all Givens are written in the past tense and all whens in the  
present tense.

I think its quite important as your feature count grows that you have  
conventions to stop getting to much step duplication

e.g.

when I create a foo with a price of £24  
when I have a foo whose price is £24  
when I have a foo priced at £24

are all the same, and whilst you could write a more complex regex to  
match all 3 (i think) it would be much better to apply a general  
convention about how you write features to deal with this

I thinks convention is probably a hangover from the old story runner  
days and isn’t quite so essential with cucumber, but it does seem to  
be quite a good way to restrict your choice of language constructions  
in your features which is a good thing if you want to have a  
consistent style

Look like I’ve got alot of Givens to rewrite 🙂

2008/11/20 David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 20, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/6 "2008-11-20T23:06:02Z")

</div>

On Thu, Nov 20, 2008 at 10:40 AM, Andrew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.

WDYT?

David

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 24, 2008, 12:16am UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/7 "2008-11-24T00:16:32Z")

</div>

“David C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > 
> 
> I actually prefer to keep these separate:  
> WDYT?
> 
> David

This is what I do.

Pat

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 23, 2008, 3:29am UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/8 "2008-11-23T03:29:24Z")

</div>

DOH  
I thinks convention = I think this convention

2008/11/21 Andrew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 24, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/9 "2008-11-24T16:56:20Z")

</div>

On Fri, Nov 21, 2008 at 4:35 AM, Andrew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Interesting … so this establishes a convention to use in features  
> that all Givens are written in the past tense and all whens in the  
> present tense.

Actually, I put both in the present tense, but Givens describe states  
(“there are…” or “\_\_\_ is…”), while Whens specify an action (“I  
create…”).

Peeja

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 24, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/10 "2008-11-24T17:50:29Z")

</div>

Yeh that just shows how bad my grammer is! Saying that ‘there is’ /  
‘there are’ is past tense, really meant ‘something that has already  
happened’ … or ‘states’ as you so concisely put it 🙂

Thanks

Andrew

2008/11/23 Peter J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![aidy\_lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@aidy\_lewis](https://www.ruby-forum.com/u/aidy_lewis)\
**Post date:** [November 24, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/cucumber-group-given-whens-and-thens/153728/11 "2008-11-24T18:02:48Z")

</div>

On Thu, Nov 20, 2008 at 8:05 PM, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> I actually prefer to keep these separate:  
> WDYT?  
> 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}”  
end

Given “an anonymous user” do  
When “I log out”  
end

When “I log in as $name” do |name|  
post ‘/sessions’, :login =\> name, :password =\> ‘monkey’  
end

When “I log out” do  
get “/logout”  
end

etc
