我的spec / controllers / undertakings_controller_spec.rb在下面。
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])
@asking=@undertaking.asking
@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 :(得分:2)
作为验收测试框架的Capybara不会暴露像请求或响应对象这样的低级细节。为了使用Capybara访问网页,开发人员需要使用方法访问(而不是get)。要读取访问的页面正文,开发人员必须使用页面而不是操作响应。
你可以阅读更多&#34; Improving the integration between Capybara and RSpec&#34;
我希望这有助于