Forum: RSpec 'And' step definition

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark W. (Guest)
on 2008-12-06 04:17
(Received via mailing list)
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
David C. (Guest)
on 2008-12-06 04:23
(Received via mailing list)
On Fri, Dec 5, 2008 at 8:16 PM, Mark W. <removed_email_address@domain.invalid> 
wrote:
>
> I'm probably missing something really obvious.

And is something the grammar allows in scenarios, but it always maps
And (and But) to the last type of Given, When or Then. So what you
really have here is a Given step that you are invoking with an alias,
And, from the Scenario.

Cheers,
David
James B. (Guest)
on 2008-12-06 04:55
Mark W. 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.
Mark W. (Guest)
on 2008-12-06 07:46
(Received via mailing list)
Thanks for your replies, David and James. I figured as much, actually,
since
you wouldn't want to have some And steps in your definition file and
some
Given steps.

I was led astray by "Inside a step_definitions.rb file, steps (which
strictly speaking should always be called step definitions) refers to
the *
matcher* methods, given exactly the same names (Given, When, Then, or
And),
each provided with a matcher regexp that corresponds to one or more
feature
steps" in James's backgrounder. Maybe I misunderstood it.

///ark
James B. (Guest)
on 2008-12-07 00:10
Mark W. wrote:
> Thanks for your replies, David and James. I figured as much, actually,
> since
> you wouldn't want to have some And steps in your definition file and
> some
> Given steps.
>
> I was led astray by "Inside a step_definitions.rb file, steps (which
> strictly speaking should always be called step definitions) refers to
> the *
> matcher* methods, given exactly the same names (Given, When, Then, or
> And),
> each provided with a matcher regexp that corresponds to one or more
> feature
> steps" in James's backgrounder. Maybe I misunderstood it.
>

That aspect of cucumber, that Given == When == Then, is elaborated later
in the article but I have added further clarification at that point as
well.
This topic is locked and can not be replied to.