参数数量错误(0 代表 1)(ArgumentError)

发布于 2024-12-06 03:58:12 字数 1266 浏览 0 评论 0原文

我使用 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 的路径来将该助手添加到 cucumber .rb 文件。

I am using devise as an authentication system and created a scenario.

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"

for step definition

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

i had also added Devise helpers to the cucumber world.

World(Devise::Controllers::Helpers)

and cucumber is generating this error:

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'

Why is it generating and how to fix it?

Update

the definition of signed_in? method is

  # 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

its defined in Devise and i added that helper to cucumber by writing this code World(Devise::Controllers::Helpers) into cucumber's paths.rb file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世俗缘 2024-12-13 03:58:12

试试这个

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

Try this instead

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

UPDATED
I think you need to do something like this

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

or
You could try modifying your feature to

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"

Then in the steps

Given /^user is not logged in$/ do
  visit('users/sign_out')  # this will ensure that the user is not logged in
end
烟若柳尘 2024-12-13 03:58:12

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

这里似乎不太可能,但是您还有其他可以匹配 And user is notlogin 但需要参数的内容吗?

I've had problems similar to this which turned out to be the test line matching another step definition somewhere else in my code.

It seems unlikely here but do you have anything else which could match to And user is not logged in but which requires an argument?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文