2016-12-27 4 views
0

My spec/controllers/undertakings_controller_spec.rbは以下のとおりです。RspecとCapybara、訪問と取得の違いは何ですか

RSpec.describe UndertakingsController, type: :controller do 
    describe 'redirect with home due to login user' do 
     subject {current_path} 
     it 'can get with authenticate undertaking user' do 
     login_user @undertaking.user 
     #get :show , id: @undertaking 
     visit undertaking_path(@undertaking) 
     expect(response).to redirect_to root_path 
     end 
    end 
end 

これはエラーです(予想される応答はaですが、<> 200)。 しかし、(undertaking_path(@undertaking)を訪問して)(get:show、id:@undertaking)に変更すると、エラーにはなりません。訪問とゲットの違いは何ですか?私は

Rspec and capybara, difference between visit and get methods, with regards to the current_path object

を読んで、私は、この場合にエラーを理解することはできません。私を助けてください。

とにかく、私のコントローラ/ undertakings_controller.rbは以下の通りです。

 class UndertakingsController < ApplicationController 
     before_action :undertaking_not_have_comment , only: [:show] 
     def show 
      @undertaking=Undertaking.find(params[:id]) 
      @[email protected] 
      @comment=Comment.new do |c| 
      c.user=current_user 
      end 
     end 

     private 
     def undertaking_not_have_comment 
      @undertaking=Undertaking.find(params[:id]) 
      if current_user == @undertaking.user 
       unless @undertaking.comments.count > 0 
       redirect_to root_path 
      end 
     end 
     end 
+0

に役立つことを願っています "であることが予想応答が、<200>だった" ですフルエラー? –

+0

申し訳ありませんが、エラーの上に "Failure/Error:expect(response).to redirect_to root_path"があります。 –

+0

'visit'は、rspecとのインテグレーション/フィーチャ仕様で使用されるカピバラのメソッドであり、' get'は一般的にコントローラ/ルーティング仕様に使用されます。これらは、テストをより読みやすくするためのDSLです。バックグラウンドでは、訪問はGETリクエストを経由します – sa77

答えて

2

Capybara, being an acceptance test framework, does not expose low-level details like a request or response object. In order to access a web page using Capybara, the developer needs to use the method visit (instead of get). To read the accessed page body, the developer must use page instead of manipulating the response.

あなたは "Improving the integration between Capybara and RSpec"

をより多くを読むことができる私は、これは

+0

リンクされた記事は完全に説明しており、Capybara :: DSLメソッドはコントローラの仕様には含めないでください。 –

+0

私は彼らが私より良く書くことに同意する – MZaragoza

+0

私はそれを見なかった...ありがとう! –

関連する問題