无法理解Michael Hart'l教程测试中的no_capybara选项 [英] Can't understand no_capybara option in michael hart'l tutorial test

查看:48
本文介绍了无法理解Michael Hart'l教程测试中的no_capybara选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在michael hart'l的Rails教程中,我无法理解规范:

In rails tutorial by michael hart'l i can't understand a spec :

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 

您看到,作者在{sign_in user,no_capybara:true}之前使用 >在每个示例之前登录用户。 no_capybara 选项声明为:

as you see, the author use before { sign_in user, no_capybara: true } to sign in user before each example. no_capybara option is declared as so :

spec / support / utilities.rb

spec/support/utilities.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请求方法时,这是必需的(他的意思是no_capybara选项)(获取,发布,修补或删除),由此我可以理解,在向用户#update动作提交PATCH请求 中,有一个 patch 方法使用,但是上面的sign_in方法与此行(修补程序方法)之间的关系是什么,据我了解,我认为之前使用 patch ,为什么我们应该添加 no_capybara

as he noted in tutorial : This is necessary (he means no_capybara option) when using one of the HTTP request methods directly (get, post, patch, or delete), i can understand by this that in the "submitting a PATCH request to the Users#update action" there is a patch method in use, but what is the relationship between sign_in method above and this line (of patch method), in my understand i think that sign_in is used before using patch so why we should add this option of no_capybara

推荐答案

我在同一部分的测试失败,并显示以下消息:

My tests to the same section were failing with these messages:

Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.

对您来说一样吗? URL为 http://www.example.com/ 对您来说并不奇怪吗?我们不在应用程序中的任何地方使用此URL。那就是水豚。就像所有其他人所说的,这意味着Capybara实际上并没有在我们的实际应用程序中创建cookie,而只是在模拟登录过程。我们将不得不在Capybara的文档中进行更多搜索,以准确了解其工作原理。但是您会看到Capybara并没有进行实际登录。现在,如果您看到我们的 users_controller.rb

Is that the same for you? Isn't it strange for you that the URL is "http://www.example.com/"? We are not using this URL anywhere in our application. That's Capybara doing it. This means that, as all the others are saying, Capybara is not actually creating a cookie in our actual application, it's just doing a simulation of a sign-in. We would have to search more in the docs of Capybara to understand exactly how it works at this point. But you see that Capybara isn't doing an actual sign in. Now, if you see our users_controller.rb:

before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]

依次执行以下操作:

def signed_in_user
  redirect_to signin_url, notice: "Please sign in." unless signed_in?
end

def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
end

因此,当测试要求:

before { get edit_user_path(wrong_user) }

之前它会被控制器自动重定向到 signin_url ,因为正如我之前说过的那样,用户并没有真正登录,也没有为他设置任何cookie。这就是为什么必须预先设置cookie的原因。

it is automatically redirected to signin_url by the controller, since, as I said before, the user is not really signed-in, no cookie has been really set for him. That is why the cookie has to be set in advance.

这篇关于无法理解Michael Hart'l教程测试中的no_capybara选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆