I’m new to Rails and Rspec and I’m using Rspec to test this controller method which includes exception handling:
def search_movies_director
@current_movie = Movie.find(params[:id])
begin
@movies = Movie.find_movies_director(params[:id])
rescue Movie::NoDirectorError
flash[:warning] = "#{@current_movie} has no director info"
redirect_to movies_path
end
end
I can’t figure out how to correctly test the said path: after invalid search (when error is received) it should redirect to the homepage. I tried something like this:
describe MoviesController do
describe 'Finding Movies With Same Director' do
#some other code
context 'after invalid search' do
it 'should redirect to the homepage' do
Movie.stub(:find)
Movie.stub(:find_movies_director).and_raise(Movie::NoDirectorError)
get :search_movies_director, {:id => '1'}
response.should redirect_to movies_path
end
end
end
end
After running the test fails with an error: NameError: uninitialized constant Movie::NoDirectorError
How to fake raising an error in this test so it actually checks whether redirect happens?
Thanks!
UPDATE:
As nzifnab explained, it couldn’t locate Movie::NoDirectorError. I forgot to define this exception class. So I added it to app/models/movie.rb :
class Movie < ActiveRecord::Base
class Movie::NoDirectorError < StandardError ; end
#some model methods
end
This solved my problem and this test passes.
The error indicates it doesn’t know where that
Movie::NoDirectorErrorexception class is defined. You might need to specificallyrequirethe file where that class is defined or the test may not be able to find it.Rails will automatically attempt to locate any
constant missingconstants using a conventional file directory format. It will look for a file in theload_pathatmovie/no_director_errorandmoviebased on the name of the constant. If the file is not found or the file doesn’t define the expected constant than you’ll need to specifically require the file yourself.