'And' step definition


#1

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


#2

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


#3

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.


#4

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


#5

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.