简体   繁体   中英

cucumber declarative step definitions using web_steps.rb

When using Cucumber for BDD (CS169.x1@edX) the following message is returned in response to execution of 'cucumber features/filter_movie_list.feature':

When I uncheck the following ratings: G, PG-13                 # features/step_definitions/movie_steps.rb:44
  Undefined step: "When I uncheck "ratings_G"" (Cucumber::Undefined)
  ./features/step_definitions/movie_steps.rb:52:in `block (2 levels) in <top (required)>'
  ./features/step_definitions/movie_steps.rb:49:in `each'
  ./features/step_definitions/movie_steps.rb:49:in `/I (un)?check the following ratings: (.*)/'
  features/filter_movie_list.feature:30:in `When I uncheck the following ratings: G, PG-13'
...
You can implement step definitions for undefined steps with these snippets:

When /^When I uncheck "(.*?)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

The particular configuration in question has a declarative step in 'features/filter_movie_list.feature' that says:

When I uncheck the following ratings: G, PG-13

an attempt to implement the declarative steps in 'features/step_definitions/movie_steps.rb' (to reuse the imperative steps of 'features/step_definitions/web_steps.rb') which looks like:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

and a working 'features/step_definitions/web_steps.rb' file (ie the one created by 'rails generate cucumber_rails_training_wheels:install' after gem installation) with an imperative default step like:

When /^(?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

Also, it seems like 'features/step_definitions/web_steps.rb' is accessible to Cucumber since adding When I uncheck "ratings_G" to 'features/step_definitions/movie_steps.rb' succeeds; anyone have any idea of what might be causing such behavior? The implementation of the declarative step has even been simplified so that no variable substitution takes place...

I've tried:

When I uncheck the following ratings: "G, PG-13"

Then in the steps file:

When /^I uncheck the following ratings: "([^"]*)"$/ do |arg1|
  puts "arg1: #{arg1}"
end

.. seems to work for me.

Problem

The problem is with how you attempt to call a step from another step.

The second line of the code:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

is looking for a step definition like:

When /^When (?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

Notice that it is looking for "When" inside the step regex. This unlikely exists, hence the undefined step error.

Solution

To call the step from a step you need to do one of the following:

1) Use step and make sure not to include the Given/When/Then keyword:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "I uncheck \"ratings_G\""

2) Or if you prefer to include Given/When/Then, use steps instead:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  steps %Q{
    When I uncheck "ratings_G"
  }

See the Cucumber wiki .

In this case the course material which was provided illucidates the difference but did not lead us to delineate between two steps that are very similar; changing "When I uncheck the following..." to "When unchecking the following..." allows web_steps.rb to come back into scope and get executed as desired (the previously linked posting, link: cucumber contradictory error messages , has more details).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM