我正在使用 rspec,并且遇到了一些问题。我在 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)>'
这是我的 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
这是我的 MovieController.rb
def samedirector
movie = Movie.find(params[:id])
@director = movie.director
end
任何帮助,将不胜感激。这是一个家庭作业问题,所以如果你能给我更多的直觉并指出我做错的潜在行而不是解决方案代码,那将非常有帮助。
更新
我根据您所说的进行了修复,然后将其初始化为
before (:each) do
@movie = Movie.create(:director => "Steven S", :title=>"Hello World")
end
但是,我现在有以下问题。似乎没有认识到导演是控制器中的史蒂文S。不知道为什么。