[rspec-users] 'And' step definition

James Byrne lists at ruby-forum.com
Fri Dec 5 21:55:55 EST 2008


Mark Wilden wrote:
> Can you have an And step in a feature? With this, I get a NoMethodError 
> for
> 'And':
> 
> Given /a company named (.+)/ do |name|
>   @company = Company.create!(:name => name)
> end
> 
> And /a user named (.+)/ do |name|
>   @user = create_user name
> end
> 
> I'm probably missing something really obvious.
> 
> ///ark


Given, When, Then and And in a feature file are all calls to the same 
code.  You can map any of these in a feature file to a matcher prefaced 
with any of Given, When and Then in a step definitions file.  My current 
practice is to only use When in the step definitions file as it reads 
(for me) more naturally.

So, in the step definitions file, if you have:

When /a company named (.+)/ do |name|
  @company = Company.create!(:name => name)
end

When /a user named (.+)/ do |name|
  @user = create_user name
end

While in the feature file you can have:

Scenario
  Given a company named Acme
    And a company named Pinnacle
  When a user named Smith
  Then a company named Zenith
    And a user named Doe

and all of these will match one of the two step definitions given above.
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list