Je la situation suivante:Comment tester un itinéraire not_found sur mesure dans des rails
ÉDITÉ
Dans mon routes.rb
namespace :api, defaults: { format: :json } do
namespace :v1 do
# the definitions of other routes of my api
# ...
match '*path', to: 'unmatch_route#not_found', via: :all
end
end
ÉDITÉ
Mon contrôleur :
class Api::V1::UnmatchRouteController < Api::V1::ApiController
def not_found
respond_to do |format|
format.json { render json: { error: 'not_found' }, status: 404 }
end
end
end
Mon test est comme le montre:
require 'rails_helper'
RSpec.describe Api::V1::UnmatchRouteController, type: :controller do
describe 'get response from unmatched route' do
before do
get :not_found, format: :json
end
it 'responds with 404 status' do
expect(response.status).to eq(404)
end
it 'check the json response' do
expect(response.body).to eq('{"error": "not_found"}')
end
end
end
Il me semble juste, mais je suis la même erreur pour les deux it
statments:
1) Api::V1::UnmatchRouteController get response from unmatched route responds with 404 status
Failure/Error: get :not_found, format: :json
ActionController::UrlGenerationError:
No route matches {:action=>"not_found", :controller=>"api/v1/unmatch_route", :format=>:json}
# /home/hohenheim/.rvm/gems/[email protected]/gems/gon-6.1.0/lib/gon/spec_helpers.rb:15:in `process'
# ./spec/controllers/api/v1/unmatch_route_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
ÉDITÉ
Le but avec cette route est déclenché quand il n'y a pas d'autre itinéraire possible dans mon API, avec une coutume j réponse 404 fils. Cette route et le contrôleur fonctionne comme prévu en ce moment, lorsque nous accédons à des routes comme: /api/v1/foo
ou /api/v1/bar
Comment puis-je écrire les tests correctement?
Informations complémentaires: Rails 4.2.6, 3.5.4 Rspec
avez-vous essayé d'ajouter comme: "not_found" dans votre itinéraire? –
si vous voulez écrire 'match '* chemin', à: 'unmatch_route # not_found', comme: 'not_found', via:: all' Je l'ai fait maintenant, mais ne fonctionne toujours pas – intmarinoreturn0