Reputation: 10376
So this is a really newby question.
I have a step that looks like this:
Then /^I should see ((phrase 1) | (phrase 2))$/ do
#stuff
end
After that, I have a step that looks like this:
Then /^I should see phrase 3$/ do
And %{I should see phrase 2}
end
I'm trying to test a registration flow.
phrase 1
describes the required information that needs to be filled out and I need to verify that they show up the in the user's profile.
phrase 3
describes the required and optional information the user fills out. The optional information should aslo show up in the user's profile and I wanna test that that also shows up.
I don't want to copy and paste the stuff from the first step, so I re-phrased phrase 1
so that it makes sense to be called from the phrase 3
step, but I don't know how to properly refer to it in the first step to get it to work. All I get from Cucumber is that I should see phrase 2
is undefined.
What's the right way to get this work? Is there a better way to accomplish my goal?
Upvotes: 3
Views: 478
Reputation: 16092
Whitespace characters are very important in regex's
With whitespace at either end of the pipe you need to use this step:
Then %{I should see phrase 1 }
or
Then %{I should see phrase 2}
(Note that extra space at the end on the first one, and between see
and phrase2
on the second one). The regex is still looking for those space characters where you specified them to be.
Your regex would need to be changed to:
Then /^I should see (phrase 1|phrase 2)$/ do |phrase_name|
end
The |
symbol in regex will match either all characters on the left side, or all characters on the right side (the parantheses of course bounds it so it doesn't take the entire string).
Also you have to pass the value to your block, as I did with phrase_name
when you use parenthesis in this manner. Although I would probably rather see the step defined this way:
Then /^I should see phrase ([\d]+)$/ do |phrase_number|
end
So that it will match for all three of your phrase types, and you wouldn't need the phrase 1 definition anymore. [\d]+ will match any numerical character 1 or more times.
Then you can do whatever you need to do based on the phrase_number that was sent. I'd be interested to see your scenario definition though, I'm not sure if you're going about cuking it correctly based on this information. Maybe I could get some context if I saw the rest of the story.
Upvotes: 4