[rspec-users] Cucumber - Recommended viewing.

Ben Mabey ben at benmabey.com
Sun Apr 12 18:17:37 EDT 2009

Matt Wynne wrote:
> On 12 Apr 2009, at 16:35, Ben Mabey wrote:
>> Matt Wynne wrote:
>>> On 11 Apr 2009, at 01:07, James Byrne wrote:
>>>> http://mwrc2009.confreaks.com/14-mar-2009-15-00-bdd-with-cucumber-ben-mabey.html 
>>> Great job with the talk Ben, it's a really good intro to Cucumber 
>>> and I will be pointing anyone who asks towards it.
>>> One question about the kitten-killing. I was surprised that defining 
>>> methods in your env / step_definition files adds methods to *every 
>>> instance* of Object. I thought it just added those methods to the 
>>> particular instance of Object that's used to create the World.
>>> Did I misunderstand the you in the talk, or misunderstand the code 
>>> in Cucumber?
>> Well, the step definitions themselves don't add themselves to every 
>> instance.  The Given, When, and Then methods have actually killed 
>> some kittens and already live on Object (sometimes it is okay).  The 
>> step methods will register the passed in blocks to the StepMother-- 
>> not onto Object.  So if that is what you are referring to you are 
>> correct.
>> However, if you want to create a ruby helper method then you will 
>> need to wrap it in a module to prevent to from being added to every 
>> object instance.  In my example I had something like:
>> def login_as(user)
>> visit '/login'
>> fill_in 'Email', :with => user.email
>> fill_in 'Password', :with => 'password'
>> click_button
>> end
>> If you place that method in the top-level in either your env.rb or 
>> any step files it is going to be living on Object since Cucumber will 
>> load up these files as any other ruby file.  So you could call 
>> #login_as on an Array or any other object!  Definitely not what we 
>> want.  So, you need to wrap it in a module and use the World hook to 
>> make the helper methods available only in your World (which you get a 
>> new one for every scenario).  With the new World syntax (as of 0.2.3 
>> I believe) it would be:
>> module UserHelpers
>> def login_as(user)
>>   visit '/login'
>>   fill_in 'Email', :with => user.email
>>   fill_in 'Password', :with => 'password'
>>   click_button
>> end
>> end
>> World(UserHelpers)
>> I feel like I may of just repeated what I said in the presentation... 
>> so you still may be just as confused. :/  Let me know if that helps 
>> to clarify things or what part of it is confusing.
> Yeah you did, but I think you've made me realise something obvious: 
> there's a difference between the ruby you write in the env and step 
> matchers, and the ruby you write *inside* the step matchers. It's the 
> later that gets evaluated in the context of the World instance, the 
> former that gets evaluated in the root namespace.
> Right?


More information about the rspec-users mailing list