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
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
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 route – sa77