Skip to content

Instantly share code, notes, and snippets.

@charissa
Created April 8, 2011 14:12
Show Gist options
  • Select an option

  • Save charissa/909910 to your computer and use it in GitHub Desktop.

Select an option

Save charissa/909910 to your computer and use it in GitHub Desktop.
Issue with regexp in cucumber created step definitions
Here is an example of what I was trying to explain last night:
Feature: tip based on customer experience
Background:
In order to decide on a tip
Customer should reflect on his experience
Scenario customer eats a meal and pays the bill and adds a tip
Given customers mood is "very happy"
When customer pays
Then tip is "0.15"
when running cucumber we get:
You can implement step definitions for undefined steps with these snippets:
Given /^customers mood is "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
Then /^tip is "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
Because of the extra " in the middle of the regexp, after filling in the step we get this error:
Feature: tip based on customer experience
Background: # features/tip.feature:2
In order to decide on a tip
Customer should reflect on his experience
Scenario customer eats a meal and pays the bill and adds a tip
Given customers mood is "very happy" # features/step_definitions/restaurant_steps.rb:117
When customer pays # features/step_definitions/restaurant_steps.rb:75
undefined method `cost' for nil:NilClass (NoMethodError)
./lib/restaurant.rb:63:in `pay'
./features/step_definitions/restaurant_steps.rb:76:in `/^customer pays$/'
features/tip.feature:8:in `When customer pays'
Because the quoted strings get all messed up.
To fix it I change the regexp to:
Given /^customers mood is "(.*)"$/ do |arg1|
and since I don't really understand regular expressions (yet) I don't know if there are any unintended side effects...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment