The simple answer is that you don't have to do this in one scenario.
You are doing two things here:
- Registering
- Signing in
Lets deal with the second first.
To sign in we have to be registered so we get a scenario like
Scenario: Sign in
Given I am registered
When I sign in
Then I should be signed in
But how do we register?
Scenario: Register
Given I am a new user
When I register
Then I should be registered
Now how do we implement this
Given "I am a new user" do
@i = create_new_user
end
When "I register" do
register as: @i
end
Then "I should be registered" do
# Go look at something to see that we are registered
end
When this works we can now implement
Given "I am registered" do
@i = create_new_user
register as: @i
end
We can do this because we have created the ability to register by getting our 'Register' scenario to work.
And now we can work on signing in
This is how BDD with Cucumber works. You work on implementing a bit of behaviour (generally in a When, e.g. registering). Then you use that behaviour (in Givens) so you can get to a place where you can work on implementing some new behaviour (signing in)
Hope thats helps
A bit more detail:
The methods create_new_user, register are called helper methods. These are key to writing simpler step definitions. In ruby you might define them as follows
module SignupStepHelper
def register
...
def create_new_user
...
end
World SignupStepHelper # makes it possible to call the methods in you step defs.