ruby-on-rails-3 - Rspec,测试更新 Controller 不起作用?

标签 ruby-on-rails-3 rspec

这是我正在努力熟悉 TDD 和 Rspec 的作业。但不知何故
我不明白为什么以下测试失败:

 describe 'update' do
    fixtures :movies
    before :each do
      @fake_movie = movies(:star_wars_movie)
    end
    it 'should retrieve the right movie from Movie model to update' do
      Movie.should_receive(:find).with(@fake_movie.id.to_s).and_return(@fake_movie)
      put :update, :id => @fake_movie.id, :movie => {:rating => @fake_movie.rating}
    end

    it 'should prepare the movie object available for update' do
      put :update, :id => @fake_movie.id, :movie => {:rating => @fake_movie.rating}
      assigns(:movie).should == @fake_movie
    end

    it 'should pass movie object the new attribute value to updated' do
      fake_new_rating = 'PG-15'
       @fake_movie.stub(:update_attributes!).with("rating" => fake_new_rating).and_return(:true)
      put :update, :id => @fake_movie.id, :movie => {:rating => fake_new_rating}
      @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true)
    end
  end

我得到的错误信息是:
Failures:

  1) MoviesController update should pass movie object the new attribute value to updated
     Failure/Error: @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true)
       (#<Movie:0xd39ea38>).update_attributes!({"rating"=>"PG-15"})
           expected: 1 time
           received: 0 times
     # ./spec/controllers/movies_controller_spec.rb:99:in `block (3 levels) in <top (required)>'

Finished in 0.60219 seconds
12 examples, 1 failure

Failed examples:

rspec ./spec/controllers/movies_controller_spec.rb:95 # MoviesController update should pass movie object the new attribute value to updated

基本上它说我的最后一行测试失败 @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true) ,我认为它根本没有收到函数调用“update_attributes!”,但为什么呢?

和 Controller 代码:
 def update
    @movie = Movie.find params[:id]
    @movie.update_attributes!(params[:movie])
    flash[:notice] = "#{@movie.title} was successfully updated."
    redirect_to movie_path(@movie)
  end

提前致谢

最佳答案

应该:

it 'should pass movie object the new attribute value to updated' do
  fake_new_rating = 'PG-15'
  Movie.stub(:find).and_return(@fake_movie)
  @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true)
  put :update, :id => @fake_movie.id, :movie => {:rating => fake_new_rating}
end

否则,行 @movie = Movie.find params[:id]将查询模型。

关于ruby-on-rails-3 - Rspec,测试更新 Controller 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9967113/

相关文章:

ruby-on-rails-3 - Rails/Rspec View : The right way to test partials

ruby-on-rails-3 - 导轨 : How to save the pdf file generated using wicked pdf

testing - 将 Rspec 模拟转换为 Mocha 以测试事件模型序列化器

ruby-on-rails - Rspec 使用 has_many throw 方法删除对象失败,没有路由匹配 { :action= >"destroy"

ruby-on-rails - `.joins` 不适用于 PostgreSQL 上 Rails 3.2 中的 has_many

ruby-on-rails - 在 Rails 4 中使用 FactoryGirl 创建模型实例时关闭观察者

ruby-on-rails - 如何使用 Rspec 正确显示双引号的 ""

ruby-on-rails-3 - 我应该将 Rails 3 观察者存储在 app/models 目录中吗?

ruby-on-rails - rails 3 : Loading a form Partial into an edit view of another model/controller

ruby-on-rails - Ruby/Rails-我可以将联接表的作用域(或类方法)用作WHERE子句的一部分吗?