RoR - тесты rspec не пройдены и не должны - PullRequest
2 голосов
/ 26 марта 2012

Я запускаю эти тесты 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 ]

1 голос
/ 16 апреля 2012

Проблема была в том, что я вызывал неправильный метод.find_by_director не тот метод, который должен был использоваться, но find_all_by_director, следовательно, то, что было в методе, было неверным.

0 голосов
/ 06 января 2014

Используйте FactoryGirl.create вместо FactoryGirl.build

.build действует как Model.new Вы должны сохранить свои тестовые записи в базе данных.

0 голосов
/ 26 марта 2012

Я предлагаю вам избавиться от насмешек и работать с «настоящими» объектами, которые сохраняются в БД:

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 .

0 голосов
/ 26 марта 2012

Сначала необходимо дать запрос с правильным методом, а затем проверить ожидаемый результат.

Поместите строку ниже в начало неудачных примеров.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...