我的规格/控制器/ undertakings_controller_spec.rb低于。Rspec和水豚,访问和获取之间有什么区别
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)时,它没有错误。访问和获得有什么区别?我读
,但我不明白在这种情况下错误。请帮帮我。
无论如何,我的控制器/ 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
是 “预期的响应是一个,但<200>” 完整的错误? –
对不起,错误上方有“Failure/Error:expect(response).to redirect_to root_path”。 –
'visit'是在rspec集成/功能规格中使用的水豚方法,'get'通常用于控制器/路由规格。这些都是DSL使测试更具可读性..在后台,访问请求获取路由 – sa77