2012-06-27 7 views

risposta

12

Quindi quello che ho scoperto è che avrei potuto fare qualcosa di simile in rspec

def other_error 
    raise "ouch!" 
end 

it "renders 500 on Runtime error" do 
    get :other_error 
    response.should render_template("errors/500") 
    response.status.should == 500 
end 
+0

Per chiarire, si sta immaginando un metodo 'other_error' su alcuni controller, ma non come un metodo di supporto nel test RSpec, giusto? Mi piacerebbe che quest'ultimo funzionasse, ma penso che sia un sogno. –

6

Ecco quello che faccio, supponendo che stai usando , e : In primo luogo, è necessario trovare un azione del controllore che chiama un metodo. Ad esempio, potresti avere un UserController con un'azione show che chiama User.find. In questo caso, si può fare qualcosa di simile:

it "should render the 500 error page when an error happens" do 
    # simulate an error in the user page 
    User.should_receive(:find).and_raise("some fancy error") 
    visit '/user/1' 

    # verify status code 
    page.status_code.should eql(500) 

    # verify layout 
    page.title.should eql('Your site title') 
    page.should have_css('navigation') 
    page.should have_css('.errors') 
end 
Problemi correlati