machineghost
machineghost

Reputation: 35790

Cucumber: What is the best practice for quoting/not quoting arguments

In Cucumber you define steps which define your BDD syntax; for instance, your test might have:

When I navigate to step 3

and then you might define a step:

When /^I navigate to step (\d+)$/ do |step_number|
   # navigate to step ${step_number}
end

Now, all of the above works perfectly fine as is (or at least I think it does). However, you can also do this instead:

When I navigate to step "3"

with a regex:

When /^I navigate to step "(\d+)"$/ do |step_number|

In "The RSpec Book: Behaviour-Driven Development with Rspec, Cucmber, and Friends", author David Chelimsky writes "There are two common styles for steps ... Discuss the pros and concs with your team". On my team a few people have already started using quotes, but it makes invoking steps manually more awkward because you have to escape the quotes inside the step step names (when those step names are themselves wrapped in quotes). However, having quotes makes it more clear where variables are in the Cucumber text.

So, what I'm wondering is: is there any sort of community consensus on what the "right" style is here? Or lacking that ...

Ideally I'd like to find out as much as I can before we write a million tests with the "wrong" style ;-)

Upvotes: 2

Views: 527

Answers (1)

socjopata
socjopata

Reputation: 5095

Seeing as no one is replying to you, I decided to comment - maybe you'll find my opinion helpful.

For example I've been using both styles extensively on a project where there was no "should do this way" in this matter. I think I ended using "(\d+)" style more, because of, like you said:

having quotes makes it more clear where variables are in

As for constructing steps that are composed of other steps, I usually did:

Then /^I fill in my profile information with: "(.*)\/(.*)\/(.*)"$/ do |display_name, picture, description|
    And %{I fill in "user_display_name" with "#{display}"}
    And %{attach the file "#{picture}" to "user_picture"}
    And %{I fill in "user_short_description" with "#{description}"}
end

Hope that helps, I am open for discussion :)

Upvotes: 4

Related Questions