ruby-on-rails-3 – 参数个数错误(0表示1)(ArgumentError)

我使用devise作为身份验证系统并创建了一个场景.

Scenario: Home page have a login page
    When I go to the home page
    And user is not logged in
    Then I should see "Sign In"

用于步骤定义

When /^user is not logged in$/ do
  signed_in? == false
end

我还为黄瓜世界添加了Devise助手.

World(Devise::Controllers::Helpers)

和黄瓜正在生成此错误:

And user is not logged in           # features/step_definitions/web_steps.rb:260
      wrong number of arguments (0 for 1) (ArgumentError)
      ./features/step_definitions/web_steps.rb:261:in `/^user is not logged in$/'
      features/manage_home_page.feature:13:in `And user is not logged in'

它为什么生成以及如何修复它?

更新

signed_in的定义?方法是

  # Return true if the given scope is signed in session. If no scope given, return
  # true if any scope is signed in. Does not run authentication hooks.
  def signed_in?(scope=nil)
    [ scope || Devise.mappings.keys ].flatten.any? do |scope| 
      warden.authenticate?(:scope => scope)
    end
  end

它在Devise中定义,我通过将此代码World(Devise :: Controllers :: Helpers)编写到cucumber的paths.rb文件中,将帮助器添加到黄瓜中.

最佳答案 试试这个

When /^user is not logged in$/ do
  user_signed_in?.should be_false
end

更新
我想你需要做这样的事情

When /^user is not logged in$/ do |user| 
  #check here user not logged in
end

要么
您可以尝试将功能修改为

Scenario: Home page has a login page
  Given I am on the home page
  And I am not logged in
  Then I should see "Sign In"

然后在步骤中

Given /^user is not logged in$/ do
  visit('users/sign_out')  # this will ensure that the user is not logged in
end
点赞