Say that I have an application where user can upload his avatar, and then he is redirected to crop page where he crops it. I might have Cucumber scenario like this one for upload
Scenario: Registered user wants to upload avatar
Given I follow "Upload picture"
When I attach the file "spec/fixtures/kitten.jpg" to "user_avatar"
And I press "Upload"
Then I should see "Picture was successfuly uploaded."
And I should be on the user crop page
and then I want to create scenario for cropping the avatar, which would directly follow the first one. It might look like this
Scenario: User just uploaded avatar
Given I just uploaded "spec/fixtures/kitten.jpg" as my avatar
When I crop it to 100x100 px
And I press "Crop"
Then my avatar should have 100x100 px
And I should see "Picture successfuly cropped."
However here I have to define the
Given I just uploaded "spec/fixtures/kitten.jpg" as my avatar
to do exactly the same thing as the previous scenario, which to me looks like huge duplication. Not to mention that if I wanted to continue with a third scenario just after this one, I’d have to duplicate the second one.
For small examples it might work to put all this in one big scenario, however even in this case it would look very ugly.
What is the correct strategy in general to make a few scenarios follow one another and pass on their state to the next one?
In general you don’t want to make tests conditional on each other. They should be able to run independently and in any order. At the point when you start coupling your tests together it’s going to start making failures harder to debug.
The Given I just uploaded “spec/fixtures/kitten.jpg” as my avatar is doesn’t need to be an exact duplicate of the first cuke. You could just as easily just create the file on the file system and any needed models in the Cucumber step. You already know the upload part works so you don’t need to do the full upload before the crop for the second example.
Or, alternatively, you could make a single step that contains:
And re-use that in the specs. That way, you can test the landing page and the flash in the first test and the cropping in the second. The values of the flash and the landing page don’t matter for the second test.