首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RoR - rspec测试失败并且不应该失败

RoR - rspec测试失败并且不应该失败
EN

Stack Overflow用户
提问于 2012-03-26 14:26:12
回答 4查看 2.2K关注 0票数 2

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

代码语言:javascript
运行
复制
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

Buy他们失败了,输出如下:

代码语言:javascript
运行
复制
    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

当这是我的控制器方法时:

代码语言:javascript
运行
复制
def similar 
    @movies = Movie.find_by_director(Movie.find_by_id(params[:id]))
  end

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-16 13:18:20

问题是我调用了错误的方法。find_by_director不是应该使用的方法,而是find_all_by_director,因此这个方法是错误的。

票数 1
EN

Stack Overflow用户

发布于 2012-03-26 14:46:20

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

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

代码语言:javascript
运行
复制
get :similar, {:id => "1"}

如下所示。

代码语言:javascript
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2012-03-26 14:48:48

我建议你摆脱mocking,使用“真正的”对象,这些对象在DB中持久存在:

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9867544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档