English 中文(简体)
cucumber features --guess
原标题:
  • 时间:2009-11-19 23:02:21
  •  标签:
  • cucumber

I m setting up my Authlogic user session features. And I m in some kind of confusion. When I run:

cucumber features 

I get some red errors

     Scenario: User signs in successfully                           # features/user_sessions.feature:21
        Given a user exists with login: "sachin", password: "secret" # features/step_definitions/pickle_steps.rb:4
        When I go to the login page                                  # features/step_definitions/webrat_steps.rb:15
        And I login as "sachin" with password "secret"               # features/step_definitions/user_session_setps.rb:5
        Then I should see "Login successful"                         # features/step_definitions/webrat_steps.rb:123
        And I should be logged in                                    # features/user_sessions.feature:26
          Ambiguous match of "I should be logged in":

          features/step_definitions/pickle_steps.rb:55:in `/^((?:(?:I|myself|me|my)|(?:(?:a|an|another|the|that) )?(?:(?:(?:(?:first|last|(?:d+(?:st|nd|rd|th))) )?(?:user))|(?:(?:user)(?::? "(?:[^"]|.)*"))))) should (?:be|have) (?:an? )?((?:duplicable|readonly|nil|store[_ ]full[_ ]sti[_ ]class|new[_ ]record|equal|present|eql|marked[_ ]for[_ ]destruction|valid[_ ]password|valid[_ ]with[_ ]callbacks|logged[_ ]in|valid[_ ]without[_ ]callbacks|respond[_ ]to[_ ]without[_ ]attributes|valid|logged[_ ]out|respond[_ ]to|instance[_ ]variable[_ ]defined|admin|blank|changed|tainted|unserializable[_ ]attribute|locking[_ ]enabled|has[_ ]role|instance[_ ]of|partial[_ ]updates|kind[_ ]of|attribute[_ ]present|is[_ ]a|frozen|invalid|acts[_ ]like|method[_ ]exists|has[_ ]attribute|disable[_ ]perishable[_ ]token[_ ]maintenance|is[_ ]haml|id|created[_ ]at|updated[_ ]at|login|crypted[_ ]password|password[_ ]salt|persistence[_ ]token|login[_ ]count|last[_ ]request[_ ]at|last[_ ]login[_ ]at|current[_ ]login[_ ]at|last[_ ]login[_ ]ip|current[_ ]login[_ ]ip|roles|first[_ ]name|last[_ ]name|perishable[_ ]token|email))$/ 
          features/step_definitions/user_session_setps.rb:13:in `/^I should be logged in$/ 

          You can run again with --guess to make Cucumber be more smart about it
           (Cucumber::Ambiguous)
          features/user_sessions.feature:26:in `And I should be logged in 

    Failing Scenarios:
    cucumber features/user_sessions.feature:7 # Scenario: User is not signed up
    cucumber features/user_sessions.feature:14 # Scenario: User enters wrong password
    cucumber features/user_sessions.feature:21 # Scenario: User signs in successfully

And as cucumber suggests, and when I run with --guess option:

cucumber features --guess 

All come green and passes. Is this the expected behavior or error??

问题回答

AFAIK, if you have 2 different step definitions that are the same, Cucumber does not know which one to run. Using the --guess flag forces Cucumber to pick the most likely one. The cucumber rake tasks use the --strict flag, and will fail if you have ambigious steps.

Essectially, you need to consider the 2 steps (one in pickle steps and one in user_session_steps) that are conflicting and make a decision as to which one to use, then delete the other one.

Consider these step definitions:

Given /Three (.*) mice/ do |disability|
  # some code
end

Given /Three blind (.*)/ do |animal|
  # some other code..
end

And a plain text step:

Given Three blind mice

Cucumber can’t make a decision about what Step Definition to execute, and will raise a Cucumber::Ambiguous error telling you to fix the ambiguity.

Guess mode

Running the plain text step will match the Regexp of both step definitions and raise Cucumber::Ambiguous. However, if you run Cucumber with --guess, it will guess that you were aiming for the step definition with 2 match groups.

There is ranking logic that gets invoked when the option is turned on:

  1. The longest Regexp with 0 capture groups always wins.
  2. The Regexp with the most capture groups wins (when there are none with 0 groups).
  3. If there are 2+ Regexen with the same number of capture groups, the one with the shortest overall captured string length wins.
  4. If there are still 2+ options then an Ambiguous error is raised.

So if you try --guess with the mice above, Cucumber will pick /Three blind (.*)/, because “mice” is shorter than “blind”.


More details here

for $ rake to work properly, specify cucumber s guess mode

in lib/tasks/cucumber.rake, specify t.cucumber_opts = "--guess":

# ...
namespace :cucumber do
  Cucumber::Rake::Task.new({:ok =>  test:prepare },  Run features that should pass ) do |t|
    t.binary = vendored_cucumber_bin # If nil, the gem s binary is used.
    t.fork = true # You may get faster startup if you set this to false
    t.profile =  default 
    t.cucumber_opts = "--guess" # <------- add this line -------<<<
  end
  # ...
end
# ...




相关问题
Why Cucumber hook methods aren t lowercase?

Cucumber has a few different hook methods like Before, After or AfterStep. I was wondering - why doesn t these method names follow Ruby s naming conventions to write method names all lowercase? ...

Cucumber Table Diff and colspan

I love cucumber, and its table diff feature. But I, often use a td colspan to display the title of the table. And I can t seem to get the table diff to work when I use colspan. (Table diff expects a ...

cucumber features --guess

I m setting up my Authlogic user session features. And I m in some kind of confusion. When I run: cucumber features I get some red errors Scenario: User signs in successfully ...

Rails | Cucumber | acl9 | AccessDenied

I am ramping up Cucumber, and I am having a issue getting one of my first tests to pass. The exception I am getting is: And I visit the new contract screen Acl9::AccessDenied (Acl9::AccessDenied) ...

Silencing Factory Girl logging

Just to clear the air, I am not some cruel factory master trying to silence working ladies. I am having a very annoying problem where when using Thoughtbot s factory girl in my specs, every time ...

How to resolve Rails model namespace collision

The story so far: I have a rails app with a model named "Term". All is well until trying to install Cucumber. Upon running rake cucumber I get Term is not a class (TypeError) This happens because ...

rspec testing views with internationalization?

I want to make sure I have the right meta desc/keyword and title text in my view so I want to create rspec views tests for that. Now the real challange here is how to make it work across multiple ...

热门标签