绝大多数我的黄瓜功能都要求用户登录.但是我并不需要为每次测试测试登录功能.我目前正在使用Devise进行身份验证.
我正在寻找一种方法来使用设计签署用户,而不填写登录表单.反正有没有这样做?我宁愿不必为每次测试都使用登录操作.
最佳答案 不,没有办法.在
documentation中,关于sign_in @user和sign_out @user帮助器方法,它说:
These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session
正如你自己所说的那样,使用before:每个块可能最干净.我喜欢像下面那样构造它:
context "login necessary" do
# Before block
before do
visit new_user_session_path
fill_in "Email", with: "test@test.com"
fill_in "Password", with: "password"
click_button "Login"
assert_contain "You logged in successfully."
end
# Actual tests that require the user to be logged in
it "does everything correctly" do
# ...
end
end
context "login not necessary" do
it "does stuff" do
# code
end
end
我发现这非常有用,因为如果我更改身份验证规则(即用户是否必须登录特定路径),我可以完成整个测试并将其移动到其他描述块中,而不更改任何更多代码.