# Sample App - TDD with Cucumber and RSpec

**URL:** <https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554>\
**Category:** RSpec\
**Created:** [December 1, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554 "2008-12-01T16:34:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 1, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/1 "2008-12-01T16:34:43Z")

</div>

Hi,

I just posted a tiny app that I put together for a presentation on  
Cucumber.

> **[GitHub - thoughtless/cuc\_demo: A simple example of TDD with Cucumber and RSpec](https://github.com/thoughtless/cuc_demo/tree/master)**
>
> master

This essentially mirrors the workflow that I strive towards. (The README  
explains more.) I don’t want to put myself out there as the standard to  
immitate, but I think it will be helpful for people just getting started  
with TDD/Cucumber/RSpec.

I’d really appreciate your feedback!

Paul

P.S. Sorry to the guys on IRC who got this message twice.

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 1, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/2 "2008-12-01T19:57:19Z")

</div>

Paul,

thanks for sharing your examples. I was just thinking about posting  
something that can be introduced nicely with your sample app.

Now in cuc\_demo we’ve got a step definition for /^there are no posties$/  
in postie\_steps.rb. This way, if we’d need to add a “Given there are no  
users” step we should create a user\_steps.rb with a similar definition.

IMHO it would be nice to have instead a “context\_definitions.rb” with  
common “givens” (similar to navigation\_definitions.rb) and there a more  
general definition, like for instance:

Given /^there are no (.\*)$/ do |thing| thing\_to\_model(thing).destroy\_all  
end

In our World we should have a thing\_to\_class method with the mappings  
between the names used in features and the model class that implement  
them.

I think this mapping is also useful if tomorrow you and/or your client  
decides to call it “pasties” instead of “posties” (and is more than  
useful if you’re writing your features in other language than English,  
cause you cannot obtain the class name through transformations of the  
original “thing”).

Cheers,  
nando

Pau C.  
escribió:\> Hi,

> I’d really appreciate your feedback!
> 
> Paul
> 
> P.S. Sorry to the guys on IRC who got this message twice.

–  
Fernando García Samblas  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

The Cocktail  
C/ Salamanca 17  
28020 Madrid  
+34 91 567 06 05

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 12:19am UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/3 "2008-12-02T00:19:17Z")

</div>

On 1 Dec 2008, at 19:00, Fernando García Samblas wrote:

> users" step we should create a user\_steps.rb with a similar
> 
> In our World we should have a thing\_to\_class method with the mappings  
> between the names used in features and the model class that  
> implement them.
> 
> I think this mapping is also useful if tomorrow you and/or your client  
> decides to call it “pasties” instead of “posties” (and is more than  
> useful if you’re writing your features in other language than English,  
> cause you cannot obtain the class name through transformations of the  
> original “thing”).

We have a convention here at Songkick (after a great deal of debate)  
that it’s OK to refer to key domain concepts like Concerts, Artists,  
Venues etc by their Ruby class name from within the scenario. Thus our  
thing\_to\_model function is always a matter of just calling  
thing.constantize.

I greatly prefer this to having any kind of custom mapping - it  
promotes the ‘ubiquitous language’ that Eric Evans talks about in the  
original DDD book.

Maybe it’s different when you’re working in another language though -  
I guess it’s preferable to use English names for classes etc, is it?

Matt W.

> **[Tea-Driven Development – Matt Wynne taking it one tea at a time](http://blog.mattwynne.net)**

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 9:04am UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/4 "2008-12-02T09:04:20Z")

</div>

Fernando GarcÃ­a Samblas wrote:

> IMHO it would be nice to have instead a “context\_definitions.rb” with  
> common “givens” (similar to navigation\_definitions.rb) and there a more  
> general definition, like for instance:

…

> Given /^there are no (.\*)$/ do |thing|

I really like that. It will make it much more clear since “Given there  
are no” will be a very common step.

Paul

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/5 "2008-12-02T13:42:20Z")

</div>

On 2 Dec 2008, at 08:04, Pau C. wrote:

> > Given /^there are no (.\*)$/ do |thing|
> 
> I really like that. It will make it much more clear since “Given there  
> are no” will be a very common step.

Hmmm, I personally think it’s safer to set up each scenario from a  
blank slate. I’ve used “Given there are no X” steps before, but they  
are just stubs to make the scenario read right, ie they simply don’t  
create anything, rather than destroying existing data.

The current implementation,  
Given /^there are no posties$/ do  
Postie.destroy\_all  
end

strikes me more of a when step, eg  
When /^all posties are destroyed$/ do  
Postie.destroy\_all  
end

A Before block to destroy the Posties (and any other models) will  
probably reduce issues in the long run if Postie began to depend on  
another object, and was trapped by a database constraint.

Ashley

–

> **[PatchSpace](http://www.patchspace.co.uk/)**

> **[影视资源聚合平台](http://www.aviewfromafar.net/)**
>
> 为您提供高清电影、电视剧、动漫和综艺内容，支持在线观看与内容筛选。

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/6 "2008-12-02T20:27:25Z")

</div>

Ashley M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> create anything, rather than destroying existing data.
> 
> A Before block to destroy the Posties (and any other models) will  
> probably reduce issues in the long run if Postie began to depend on  
> another object, and was trapped by a database constraint.
> 
> Ashley

I’m with you on this. In fact, I think my implementation would be

Given …  
Postie.count.should == 0  
end

It’s just an assertion (in the C sense of the word, not testing). You  
should be starting off with a clean slate anyway…but having a couple  
assertions before you run can be very helpful for localization. Plus  
this step can obviously be reused as a When.

Pat

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/7 "2008-12-02T15:29:33Z")

</div>

Matt W.  
escribió:\> Maybe it’s different when you’re working in another language though -

> I guess it’s preferable to use English names for classes etc, is it?

Exactly Matt that’s it. I forgot to explain that, thanks for mention it.

We use pure English in our code and Spanish in the features.

A business term in Spanish can have a bunch of possible translations to  
English. The mapping functions let us now exactly what are we talking  
about, reducing ambiguity and improving communication with business  
people.

> Matt W.  
> [http://blog.mattwynne.net](http://blog.mattwynne.net)  
> [http://www.songkick.com](http://www.songkick.com)
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

–  
Fernando García Samblas  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://nando.lacoctelera.com](http://nando.lacoctelera.com)

The Cocktail  
C/ Salamanca 17  
28020 Madrid  
+34 91 567 06 05

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 3, 2008, 11:39am UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/8 "2008-12-03T11:39:25Z")

</div>

On 2 Dec 2008, at 19:25, Pat M. wrote:

> Given …  
> Postie.count.should == 0  
> end
> 
> It’s just an assertion (in the C sense of the word, not testing). You  
> should be starting off with a clean slate anyway…but having a couple  
> assertions before you run can be very helpful for localization. Plus  
> this step can obviously be reused as a When.

Nice idea! That kind of sanity checking can’t hurt.

Ashley

–

> **[PatchSpace](http://www.patchspace.co.uk/)**

> **[影视资源聚合平台](http://www.aviewfromafar.net/)**
>
> 为您提供高清电影、电视剧、动漫和综艺内容，支持在线观看与内容筛选。

---

<div class="post-metadata">

**Author:** ![phylae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@phylae](https://www.ruby-forum.com/u/phylae)\
**Post date:** [December 2, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/sample-app-tdd-with-cucumber-and-rspec/154554/9 "2008-12-02T22:13:41Z")

</div>

Ashley M.  
escribió:\> are just stubs to make the scenario read right, ie they simply don’t

> create anything, rather than destroying existing data.

Yes, I also agree with you and Pat. Givens should create the scenario  
context, not clean the garbage left by others.

But I was using it just as an example of “reusing” common Givens (after  
all it was the only Given in Paul’s example app), the same way we do  
with Whens a Thens.

Take this example instead:

Given /^there is (?:a|an) (.\*)$/ do |thing|  
@resources ||=   
@resources \<\< thing\_to\_model(thing).create  
end

Using a model factory like FixturesReplacement you can deal with  
required data. WDYT?

> A Before block to destroy the Posties (and any other models) will  
> probably reduce issues in the long run if Postie began to depend on  
> another object, and was trapped by a database constraint.
> 
> Ashley

–  
Fernando García Samblas  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://nando.lacoctelera.com](http://nando.lacoctelera.com)

The Cocktail  
C/ Salamanca 17  
28020 Madrid  
+34 91 567 06 05
