1

我在 Rails 中的 Rspec 测试收到以下错误

1) MoviesController searching for similar movies should find the similar movies by director
     Failure/Error: get :similar, {:id => "1"}
     ActiveRecord::RecordNotFound:
       Couldn't find Movie with id=1
     # ./app/controllers/movies_controller.rb:63:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:16:in `block (3 levels) in <top (required)>'

  2) MoviesController searching for similar movies should select the Similiar Movies template for rendering
     Failure/Error: get :similar, {:id => "1"}
     ActiveRecord::RecordNotFound:
       Couldn't find Movie with id=1
     # ./app/controllers/movies_controller.rb:63:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:22:in `block (3 levels) in <top (required)>'

  3) MoviesController searching for similar movies it should make the results available to the template
     Failure/Error: get :similar, {:id => "1"}
     ActiveRecord::RecordNotFound:
       Couldn't find Movie with id=1
     # ./app/controllers/movies_controller.rb:63:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:29:in `block (3 levels) in <top (required)>'

这是我的控制器方法导致它失败:

def similar
  @movie = Movie.find(params[:id])
  @movies = Movie.find_all_by_director(Movie.find_by_id(params[:id])[:director])
  if @movies.count <= 1
    redirect_to movies_path
    flash[:notice] = "'#{@movie.title}' has no director info"
end

我不明白为什么这个测试总是以同样的错误失败。任何帮助将非常感激

下面是测试

describe MoviesController do
  describe 'searching for similar movies' do
    before :each do 
      @fake_movies = [mock('Movie'), mock('Movie')]
      @fake_movie = FactoryGirl.build(:movie, :id => "1", :title => "Star Wars", :director => "George Lucas") 
end
it 'should follow the route to the similar movies by director page' do
  assert_routing('movies/1/similar', {:controller => 'movies', :action => 'similar', :id => '1'}) 
end

it 'should find the similar movies by director' do
  Movie.should_receive(:find_by_id).with("1").and_return(@fake_movie)
  Movie.should_receive(:find_all_by_director).with(@fake_movie.director).and_return(@fake_movies)
  get :similar, {:id => "1"}
end

it 'should select the Similiar Movies template for rendering' do
  Movie.should_receive(:find_by_id).with("1").and_return(@fake_movie)
  Movie.should_receive(:find_all_by_director).with(@fake_movie.director).and_return(@fake_movies)
  get :similar, {:id => "1"}
  response.should render_template('similar')
end

it 'it should make the results available to the template' do
  Movie.should_receive(:find_by_id).with("1").and_return(@fake_movie)
  Movie.should_receive(:find_all_by_director).with(@fake_movie.director).and_return(@fake_movies)
  get :similar, {:id => "1"}
  assigns(:movies).should == @fake_results
end
end
end

FactoryGirl 的设置如下:

FactoryGirl.define do
  factory :movie do
    title 'Star Wars' 
    director 'George Lucas'
  end
end
4

1 回答 1

4

规范调用Factory.build. 这不会将对象持久化到数据库中。你会想要使用Factory.create来允许Movie.find工作。

于 2012-06-25T20:02:27.753 回答