1
votes

My spec/controllers/undertakings_controller_spec.rb is below.

 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

This has error (Expected response to be a , but was <200>). But when I change ( visit undertaking_path(@undertaking) ) to ( get :show , id: @undertaking ) , this does not have the error. What is the difference between visit and get? I read

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

but I can't understand the error in this case. Please help me.

Anyway, My controllers/undertakings_controller.rb is below.

      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
1
Is "Expected response to be a , but was <200>" the full error?Jagdeep Singh
Sorry, there is "Failure/Error: expect(response).to redirect_to root_path" above the error.HAYATO SHIBA
visit is a capybara method used in integration/feature specs with rspec, get is generally used for controller/routing specs. these are DSL to make the tests more readable.. in the background, visit does a GET request to the routesa77
Oh,I see! Thank you for your precise summary!HAYATO SHIBA

1 Answers

3
votes

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.

you can read more "Improving the integration between Capybara and RSpec"

I hope that this helps