错误的参数个数(0表示1)(ArgumentError)

时间:2011-09-23 09:51:04

标签: ruby-on-rails-3 cucumber

我使用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)写入黄瓜的paths.rb文件中,将帮助器添加到黄瓜中。

2 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

我遇到类似的问题,结果证明我的代码中的其他步骤定义与另一个步骤定义相匹配。

这里似乎不太可能,但你还有其他任何可以匹配And user is not logged in但需要参数的东西吗?