2013-04-17 9 views
9

ho questo percorso (certamente orrendo) in Rails:Come richiedere (GET/POST) percorsi in RSpec che hanno un carattere jolly

 
scope '/software' do 
    post '/:software_id/:attachment_id/event/*event' => 'software#post_event', as: 'post_event' 
end 

(vorrei cambiarlo, ma per un API legacy)

E sto scrivendo un test RSpec per questo.

rake routes mi da:

 
post_event POST /software/:software_id/:attachment_id/event/*event(.:format)  api/version1301/software#post_event 

Il mio test è simile al seguente:

 
    describe "post_event" do 

    it "should respond with 204" do 
     params = { 
     attachment_id: @attachment.uid, 
     software_id: @license.id 
     } 

     post :post_event, params 

     response.code.should eq "204" 
    end 
    end 

Ma ottengo il seguente errore di routing:

 
Failure/Error: post :post_event, params 
ActionController::RoutingError: 
No route matches {:format=>"json", :name_path=>:api, :attachment=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :software_id=>"0000001", :attachment_id=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :controller=>"api/version1301/software", :action=>"post_event"} 
    # ./spec/controllers/api/version1301/software_controller_spec.rb:62:in `block (4 levels) in ' 

Come si fa a gestire il percorso con il jolly (evento)?

risposta

11

(Rispondendo alla mia domanda)

Questo si è rivelato essere un po 'un errore 'rookie'.

La parte jolly (evento) del percorso richiede ancora un parametro. Non stavo passando il parametro 'evento', quindi la rotta era incompleta.

Il seguente codice funziona:

 
describe "post_event" do 

    it "should respond with 204" do 
    params = { 
     attachment_id: @attachment.uid, 
     software_id: @license.id, 
     event: 'some/event' 
    } 

    post :post_event, params 

    response.code.should eq "204" 
    end 
end 

/*event(.:format) significa che è in attesa di un parametro.

Nota speciale per sé e degli altri:

Se avete mai errori di routing in Rails, verificare che si sta passando tutti i parametri.

+2

Si è verificato un problema simile con un motore montato all'interno di un ambito: scope "* lang" {mount XX :: Engine, at: '/'}. Ho dovuto passare in 'lang: "en"' al 'get' nel test per farlo funzionare .... mi ci sono voluti 2 giorni per trovare ..... –

+0

Ultimamente ho scritto [specifiche di routing ] (https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs) per tutti i percorsi non RESTful che incontro. Ha salvato un po 'di mal di testa. – Epigene

Problemi correlati