ruby-on-rails - Rspec 在 rails 上失败

标签 ruby-on-rails ruby ruby-on-rails-3 rspec

我在 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)>'

这是导致它失败的我的 Controller 方法:

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

最佳答案

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

关于ruby-on-rails - Rspec 在 rails 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192131/

相关文章:

html - 简单的 HTML 表格 td 间距 - Rails

ruby-on-rails - 需要编程方法来检测 has_many 和 has_one 关系

ruby-on-rails - 将 after_validation 与 if 或子句一起使用

ruby - diff 一个 ruby​​ 字符串或数组

ruby - 使用 Ruby 的全功能 REST 服务器?

ruby-on-rails - Rails 范围 : Select Distinct Title With Values

ruby - Shoes.rb 可以创建独立的应用程序吗?

postgresql - Postgres 序列跳跃 32

ruby-on-rails - 访问 Rails 中的公共(public)文件

ruby-on-rails - Rails 3 Action 邮件程序 Gmail