ard*_*vis 9 integration-testing ruby-on-rails cucumber devise
注意:我对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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
似乎可能有一种方法来使用设计'current_user'帮助器.
访问登录用户的正确方法是什么?
如果您需要更多信息,请与我们联系.谢谢!
更新1: 我通过创建允许我这样做的新步骤暂时解决了我的问题:
Given I have already created a note that is owned by the user with email "test@email.com"
Run Code Online (Sandbox Code Playgroud)
但我不想指定电子邮件,如果可能的话,我仍然希望能够使用登录用户.
更新2: 添加了general_steps.rb
所以你可以看到,在我的'Background'中,用户是通过Factory创建的,然后通过我的界面登录.我想访问登录用户的模型.
我不使用 Devise,所以我无法具体回答 Devise 是否有访问current_user.
但我实际上喜欢使用Pickle来帮助我保留我的参考资料。也许这可以帮助您,直到您找到一种更具体的设计方法来实现您想要的目标。
Given /^the following (.+) records$/ do |factory, table|
table.hashes.each do |hash|
Factory(factory, hash)
# since this is like an all encompassing factory creator, this line to
# create a Pickle references might need a bit more extra work if you want
# to create references for other factory types
# I assume email is unique, else use a unique identifier of some sort
find_model! %{user: "#{hash['email']}"}, {:email => hash['email']}
end
end
Given /^I have already created a (.+) that belongs to #{capture_model}$/ do |factory, name|
model = Factory(factory)
ref = model!(name) # we find that reference here
ref.send(model.class.to_s.downcase.pluralize) << model
end
Run Code Online (Sandbox Code Playgroud)
这将读取
Given I have already created a note that belongs to user: "test@email.com"
# I would just change this to
Given I have already created a note
# or
Given a note was created for user: "test@email.com"
Run Code Online (Sandbox Code Playgroud)
你I既然说了Given I logged in...,就不用说that belongs to user: "test@email.com"已经了you。
更不用说它可能会导致您阅读时感到困惑,有些人可能会认为您正在向用户添加注释,而他们现在可能知道(或意识到)实际上是您自己。
虽然您仍然必须明确引用(例如用户:“John Doe”),但我认为这是一个优点。通过始终调用特定参考,每个人都知道谁被参考,并且不存在谁在做什么的问题。
Pickle 非常适合我们这个目的。我们发现的唯一有问题的区域是直接通过应用程序的用户界面创建的内容,这会有点棘手,以确保您创建对其的正确引用。
Pickle 有很多用途,所以一定要看看。
乌帕特
你必须在这里找到自己。因为,就像您想要的那样,没有current_user选择(据我们所知)。所以你基本上必须去寻找这个场景中的“演员”。
Given /^I have already created a note$/ do
user = model!(%{user: "test@email.com"}) # if using pickle
# user = User.find_by_email("test@email.com") # if just regular AR
user.notes << Note.create
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4875 次 |
| 最近记录: |