I’m using rspec, and having some problems. I’m getting the following error in rspec
1) MoviesController find movies with same director should call the model method that searches for movie by director
Failure/Error: get :samedirector, {:id => 1}
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
# ./app/controllers/movies_controller.rb:63:in `samedirector'
# ./spec/controllers/movies_controller_spec.rb:11:in `block (3 levels) in <top (required)>'
This is my movies_controller_spec.rb
describe MoviesController do
describe 'find movies with same director' do
let(:movie) { Movie.create }
before {movie.id=1, movie.director = "Steven S", movie.title="Hello World"}
subject { movie }
it 'should call the model method that searches for movie by director' do
Movie.should_receive(:find).with("1")
#this get is passing a nil id
get :samedirector, {:id => movie.id}
end
end
end
and this is my MovieController.rb
def samedirector
movie = Movie.find(params[:id])
@director = movie.director
end
Any help would be appreciated. This is a homework problem, so if you could give me more of the intuition and point to potential lines I’m doing wrong than solution code, that would be really helpful.
Update
I fixed it based on what you said, and just initialized it in
before (:each) do
@movie = Movie.create(:director => "Steven S", :title=>"Hello World")
end
However, I now have the following problem. Seems to be it isn’t recognizing that the director is Steven S in the controller. Not sure why.
When you do
you’re changing what happens when you do
Movie.find(1). This replaces the orignal implementation (ieshould_receiveisn’t a test spy). In particular, the return value is nil (by default). This then breaks your app code, since it’s not expectingMovie.findto return nilYou can use
and_returnto control what value should be returned