在 michael hart'l 的 Rails 教程中,我无法理解规范:
require 'spec_helper'
describe "Authentication" do
.
.
.
describe "authorization" do
.
.
.
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_title(full_title('Edit user')) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
如您所见,作者before { sign_in user, no_capybara: true }
在每个示例之前使用登录用户。no_capybara
选项声明如下:
规范/支持/实用程序.rb
.
.
.
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
正如他在教程中指出的那样:当直接使用其中一种 HTTP 请求方法(get、post、patch 或 delete)时,这是必要的(他的意思是 no_capybara 选项),我可以理解这一点,在“向Users#update action”有一个 补丁方法在使用,但是上面的sign_in方法和这一行(补丁方法)之间有什么关系,据我了解,我认为在使用补丁之前使用了sign_in ,所以为什么我们应该添加这个选项no_capybara _