Cucumber can't find steps when running a singl

2019-04-16 05:43发布

I've just installed cucumber into a new rails project (first time setting it up from scratch) and it works wonderfully when running all tests (bundle exec cucumber) but can't find any of my steps when I run a single feature file. How might I start to debug this?

rails (3.2.13)
cucumber-rails (1.3.1)
cucumber (>= 1.2.0)

# file listing
features/
├── campaigns
│   ├── donating_campaigns.feature
│   └── viewing_campaigns.feature
├── step_definitions
│   └── campaign_steps.rb
└── support
    └── env.rb

3条回答
叼着烟拽天下
2楼-- · 2019-04-16 06:01

I think you may be misunderstanding what step definitions are. In features/step_definitions is where you define your steps. So it looks like you have two features that contain scenarios you will or have written. Run the scenarios and you will see the step definitions you need to define. one for each line of your scenario. Now you need to define the steps in your campaign_steps.rb file using Capybara. For a fictitious example,

Given /^a user visits the campaign page$/ do
  visit campaign_path
end

When /^the user submits valid campaign information$/ do
  fill_in "Email",    with: @user.email
  fill_in "Donation", with: @user.donation 
  click_button "donate"
end

Cucumber used to come with web_steps.rb that defined many steps for you. However, that was changed and left out in recent versions. Hope this gets you started in the right direction. Stick with it

查看更多
Lonely孤独者°
3楼-- · 2019-04-16 06:14

I had this problem in one of my projects. When I compared the cucumber.yml file of a functional project with the problematic one, I found that the functional file set -r features on the std_opts and rerun assignments. When I added that to the broken project, Cucumber could find my step definitions.

Here's my successful cucumber.yml file:

<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip -r features"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip -r features

Kudos to this coder for pointing me in the right direction: http://web.archive.org/web/20121026012412/http://old.thoughtsincomputation.com/posts/cucumber-step-definitions-and-autorequire-hell

查看更多
甜甜的少女心
4楼-- · 2019-04-16 06:22

It will only find files at the same level or lower in the features directory tree. So if you try to run just the scenarios in features/campaigns/donating_campaigns.feature it cannot find the step_definitions directory.

You can use the --require flag to explicitly tell cucumber what to include before it runs your feature. For example:

bundle exec cucumber --require features/step_definitions features/campaigns/donating_campaigns.feature
查看更多
登录 后发表回答