【发布时间】:2011-06-10 12:56:35
【问题描述】:
注意:我对 Cucumber 很陌生。
我正在尝试做一个通用步骤(不确定某个地方是否已经存在),以便在存在关联的情况下,您可以轻松地将对象添加到另一个对象。我想做类似的事情:
manage_notes.feature
Background: Login User
Given the following user records
| email | password |
| test@email.com | password |
Given I am logged in as "test@email.com" with password "password"
Scenario: Edit existing note
Given I have already created a note that belongs to current_user
general_steps.rb
Given /^the following (.+) records?$/ do |factory, table|
table.hashes.each do |hash|
Factory(factory, hash)
end
end
Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |email, password|
unless email.blank?
visit new_user_session_path
fill_in "Email", :with => email
fill_in "Password", :with => password
click_button "Sign In"
end
end
note_steps.rb
Given /^I have already created a (.+) that belongs to (.+)$/ do |factory, resource|
model = Factory(factory)
resource.send(model.class.to_s.downcase.pluralize) << model
end
似乎有一种方法可以使用 devise 'current_user' helper。
访问已登录用户的正确方法是什么?
如果您需要更多信息,请告诉我。 谢谢!
更新 1: 我通过创建一个允许我执行以下操作的新步骤暂时解决了我的问题:
Given I have already created a note that is owned by the user with email "test@email.com"
但我不想指定电子邮件,如果可能的话,我仍然希望能够使用已登录的用户。
更新 2: 添加了general_steps.rb
所以你可以看到,在我的“后台”中,用户是通过工厂创建的,然后通过我的界面登录。我想访问该登录用户的模型。
【问题讨论】:
标签: ruby-on-rails devise cucumber integration-testing