Ruby on rails RSpec测试,如果accept标头的格式不同,则捕获406个错误
我试图编写一个RSpec测试来测试传入请求的内容类型是否错误,因为在控制器端我只支持JSON 所以目前我的RSpec是这样写的:Ruby on rails RSpec测试,如果accept标头的格式不同,则捕获406个错误,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我试图编写一个RSpec测试来测试传入请求的内容类型是否错误,因为在控制器端我只支持JSON 所以目前我的RSpec是这样写的: it 'returns a 406 if the request is not for json' do request.accept = 'text/html' get :show expect(response.code).to eq('406') end respond_to :json def show org_id = Orga
it 'returns a 406 if the request is not for json' do
request.accept = 'text/html'
get :show
expect(response.code).to eq('406')
end
respond_to :json
def show
org_id = Organization.find_by_id(params[:id])
@organization = Organization.includes([:BunchOfTablesBlah).find(params[:id])
respond_with(@organization)
end
我的控制器中的显示方法如下所示:
it 'returns a 406 if the request is not for json' do
request.accept = 'text/html'
get :show
expect(response.code).to eq('406')
end
respond_to :json
def show
org_id = Organization.find_by_id(params[:id])
@organization = Organization.includes([:BunchOfTablesBlah).find(params[:id])
respond_with(@organization)
end
但该测试因以下错误而失败:
1) PopulationManagementController显示组织返回406
如果请求不是针对json的
失败/错误:获取:显示
ActionController::路由错误:
没有路由匹配{:controller=>“population\u management”,:action=>“show”}
#/规格/控制器/人口管理控制器规格rb:184:in
`'中的块(3层)'
ActionController#show需要一个ID,但您尚未提供ID,因此路由失败
尝试
get:show,:id=>“1”
是的。。。但它是在测试中,所以我和FactoryGirl一起确定了一个id,并在你放“1”的地方通过它。。。工作。谢谢