黄瓜新问题

时间:2010-08-15 07:02:04

标签: ruby-on-rails ruby-on-rails-3 cucumber

我无法在黄瓜中显示模拟登录用户。

当用户登录时,他们可以发帖。

我的错误:

(::) failed steps (::)

undefined local variable or method `user' for #<Cucumber::Rails::World:0x85974df4>    (NameError)
./features/step_definitions/tasklist_steps.rb:25:in `/^that I want to post a link$/'
features/tasklist.feature:10:in `Given that I want to post a link'

Failing Scenarios:
cucumber features/tasklist.feature:9 # Scenario: Submitting a Link

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 1 undefined, 1 passed)
0m0.432s

我的黄瓜:

Given /^I am an authenticated user$/ do
  name = 'example'
  email = 'example@example.com'
  password = 'secret'

  Given %{I have one user "#{name}" with email "#{email}" and password "#{password}"}
  And %{I go to the user login page}
  And %{I fill in "username" with "#{name}"}
  And %{I fill in "password" with "#{password}"}
  And %{I press "Login"}
end

Given /^that I want to post a link$/ do
  title = 'My revolutionary post'
  website_link = "http://www.google.com"
  category = 'activism'
  user = authenticated user
  And %{I go to the new post page(#{user})}

如何模拟经过身份验证的用户?那么测试会通过吗?

2 个答案:

答案 0 :(得分:1)

该行

user = authenticated user

是问题所在。右侧符号user未定义。 authenticated是一种方法吗?

更新:问题澄清

在你的Cucumber功能中,你会遇到一个类似这样的场景:

Scenario: Post a link as an authenticated user
  Given I am logged in as the following user:
    | name     | John Doe |
    | email    | john@example.com |
    | password | secret |
  When I go to the posts page
  And I follow "New post"
  And I fill in the following:
    | title    | My revolutionary post |
    | category | Activism |

  etc

然后创建一个这样的步骤定义:

Given /^I am logged in as the following user:$/ do |fields|
  @user = User.create!(fields.rows_hash)
end

现在@user将在后续步骤定义中提供,以完成该方案。如果您需要为用户提供默认的模型实例值(即除了场景中显示的3之外),那么您应该考虑使用Factory Girl gem,在这种情况下您可以像这样创建用户:

@user = Factory(:user, fields.rows_hash)

答案 1 :(得分:0)

好像你期待

user = authenticated user

意思是什么。至少从你提供的内容来看,它看起来不会做任何事情,除非它应该是一个名为“authenticated”的方法,它返回一个用户对象。

相关问题