【问题标题】:RoR: Wrong Number of ArgumentsRoR:参数数量错误
【发布时间】:2025-12-19 08:05:07
【问题描述】:

我试图在我的 Rails 3 代码中调用一个方法,但得到:

失败/错误:integration_sign_in wrong_user 参数错误: 参数数量错误(0 代表 1)

这是调用代码(在 RSpec 帮助器中):

before(:each) do
    wrong_user = Factory(:user, :email => "test@test.com", :password=>"hellohello", :password_confirmation => "hellohello")
    integration_sign_in wrong_user
end

所以它显然传递了一个论点。如果参数由于某种原因为空,是否会使其认为它不是参数?

相关背景故事:为了测试,我刚从 webrat 切换到 capybara。正如Railscast 257 中所建议的,我还安装了launchy 和database_cleaner gem。当我使用 webrat 时,上面的代码按预期工作,但现在(我相信与 database_cleaner 相关)出了点问题。

可能相关:在我的 spec_helper.rb 中,我改为: config.use_transactional_fixtures = false(即使 'true' 也有同样的问题)

有什么想法吗?谢谢。

【问题讨论】:

  • 我已经回答了下面的问题,但后续:为什么堆栈跟踪错误?这让它变得非常混乱。是否因为该方法位于 spec_helper.rb 文件中,堆栈无法到达(抓住稻草)?

标签: ruby-on-rails ruby integration-testing capybara


【解决方案1】:

看起来参数错误实际上不在此函数中(即使堆栈顶部看起来确实如此。实际错误在 integration_sign_in 函数内部:

def integration_sign_in(user)
        visit signin_path
        fill_in :email,    :with => user.email
        fill_in :password, :with => user.password
        click_button
    end

看起来click_button 在 capybara 中需要一个参数,但在 webrat 中不需要。

【讨论】:

  • 这是正确的,因为 Capybara 不确定您要点击哪个按钮。
【解决方案2】:
integration_sign_in(wrong_user)

【讨论】:

  • 我也有这个 - 没有骰子。谢谢。