开发者

Ambiguous step in cucumber

开发者 https://www.devze.com 2023-04-03 18:30 出处:网络
I have defined this step: Then /^the \"([^\"]*)\" field(?: within (.*))? should be empty$/ do |field, parent|

I have defined this step:

Then /^the "([^"]*)" field(?: within (.*))? should be empty$/ do |field, parent|
  with_scope(parent) do
    field = find_field(field)
    field_value = (field.tag_name == 'textarea') ? field.text : field.value
    if field_value.respond_to? :shou开发者_开发问答ld
      field_value.to_s.should == ''
    else
      assert_equal('', field_value.to_s)
    end
  end
end

and when I run my scenario it raise the following error:

Then the "City" field within the "Buying a House" form should be empty # Ambiguous match of "the "City" field within the "Buying a House" form should be empty":

  features/step_definitions/my_steps.rb:1:in `/^the "([^"]*)" field(?: within (.*))? should be empty$/'
  features/step_definitions/web_steps.rb:35:in `/^(.*) within (.*[^:])$/'

The step mentioned is as following:

When /^(.*) within (.*[^:])$/ do |step, parent|
  with_scope(parent) { When step }
end

So, I have a lot questions...

  • How could a possible define my step without matching that? When /^(.*) within (.*[^:])$/ seems absolutely inevitable.

  • However, this step doesn't cause ambiguity:

features/step_definitions/web_steps.rb:141

Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
  with_scope(parent) do
    field = find_field(field)
    field_value = (field.tag_name == 'textarea') ? field.text : field.value
    if field_value.respond_to? :should
      field_value.should =~ /#{value}/
    else
      assert_match(/#{value}/, field_value)
    end
  end
end
  • Couldn't I just remove the When /^(.*) within (.*[^:])$/ step? Is it part of "the system"? I mean, it IS into /step_definitions/web_steps.rb, and it comes with rake cucumber:install.


If fine to edit web_steps.rb. In fact, the latest version Cucumber suggests deleting it entirely:

# TL;DR: YOU SHOULD DELETE THIS FILE
#
# This file was generated by Cucumber-Rails and is only here to get you a head start
# These step definitions are thin wrappers around the Capybara/Webrat API that lets you
# visit pages, interact with widgets and make assertions about page content.
# If you use these step definitions as basis for your features you will quickly end up
# with features that are:
#
# * Hard to maintain
# * Verbose to read
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号