Advertisement
akaiiro

Rspec: Test that controller rescues error

Jun 11th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.76 KB | None | 0 0
  1. class SomeController < ApplicationController
  2.   def some_action
  3.       Model.find(params[:id])
  4.   rescue ActiveRecord::RecordNotFound
  5.       redirect_to {:controller => some_controller, :action_some_action}
  6.   end
  7.  
  8. end
  9.  
  10. # Basic
  11. it 'catches RecordNotFound error' do
  12.    error = ActiveRecord::RecordNotFound.new
  13.    expect {
  14.      Model.should_receive(:find).and_raise(error)
  15.      post :show, { :id => 2 }
  16.    }.to_not raise_error
  17. end
  18.  
  19. # Better: Test for the actions expected when rescuing the error
  20. it 'redirects to some_controller#some_action' do
  21.    error = ActiveRecord::RecordNotFound.new
  22.    Model.should_receive(:find).and_raise(error)
  23.  
  24.    post :show, { :id => 'something that does not exist' }
  25.    response.should redirect_to('/wherever-it-should')
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement