RoR - rspec测试失败,不应该

时间:2012-03-26 06:26:12

标签: ruby-on-rails ruby unit-testing rspec

我正在为我的控制器运行这些rspec测试:

require 'spec_helper'

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_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_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_by_director).with(@fake_movie.director).and_return(@fake_movies)
      get :similar, {:id => "1"}
      assigns(:movies).should == @fake_results
    end
  end
end

购买他们失败了这个输出:

    Failures:

  1) MoviesController searching for similar movies should find the similar movies by director
     Failure/Error: get :similar, {:id => "1"}
       <Movie(id: integer, title: string, rating: string, description: text, release_date: datetime, created_at: datetime, updated_at: datetime, director: string) (class)> received :find_by_director with unexpected arguments
         expected: ("George Lucas")
              got: ()
     # ./app/controllers/movies_controller.rb:62:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:17: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"}
       <Movie(id: integer, title: string, rating: string, description: text, release_date: datetime, created_at: datetime, updated_at: datetime, director: string) (class)> received :find_by_director with unexpected arguments
         expected: ("George Lucas")
              got: ()
     # ./app/controllers/movies_controller.rb:62:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:23: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"}
       <Movie(id: integer, title: string, rating: string, description: text, release_date: datetime, created_at: datetime, updated_at: datetime, director: string) (class)> received :find_by_director with unexpected arguments
         expected: ("George Lucas")
              got: ()
     # ./app/controllers/movies_controller.rb:62:in `similar'
     # ./spec/controllers/movies_controller_spec.rb:30:in `block (3 levels) in <top (required)>'

Finished in 0.15517 seconds
4 examples, 3 failures

Failed examples:

rspec ./spec/controllers/movies_controller_spec.rb:14 # MoviesController searching for similar movies should find the similar movies by director
rspec ./spec/controllers/movies_controller_spec.rb:20 # MoviesController searching for similar movies should select the Similiar Movies template for rendering
rspec ./spec/controllers/movies_controller_spec.rb:27 # MoviesController searching for similar movies it should make the results available to the template

这是我的控制器方法:

def similar 
    @movies = Movie.find_by_director(Movie.find_by_id(params[:id]))
  end

我不明白为什么这些测试失败了。

4 个答案:

答案 0 :(得分:1)

问题是我在调用错误的方法。 find_by_director不是应该使用的方法,而是find_all_by_director,因此该方法的内容是错误的。

答案 1 :(得分:0)

您需要先使用适当的方法提出请求,然后测试预期的结果。

将以下行放在失败示例的开头。

get :similar, {:id => "1"}

如下所示。

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

答案 2 :(得分:0)

我建议你摆脱嘲弄并使用DB中持久存在的“真实”对象:

describe MoviesController do
  describe 'searching for similar movies' do
    before do 
      @movie = FactoryGirl.create(:movie,
        :title => "Star Wars",
        :director => "George Lucas") 

      @another_lucas_movie = FactoryGirl.create(:movie,
        :title => "Indiana Jones and the Last Crusade",
        :director => "George Lucas")
    end

    it 'should find the similar movies by director' do
      get :similar, {:id => @movie.id}
      assigns(:movies).should include @another_lucas_movie
    end
  end
end

有关详细信息,请参阅RSpec docs

答案 3 :(得分:0)

使用FactoryGirl.create代替FactoryGirl.build

.build充当Model.new您必须将测试记录保存到数据库。