使用find_by_id在RSpec中获取不存在的记录时引发RecordNotFound

我在products_controller_spec.rb中编写了这个规范,用于在不存在的记录上调用destroy时测试重定向:

it "deleting a non-existent product should redirect to the user's profile page with a flash error" do delete :destroy, {:id => 9999} response.should redirect_to "/profile" flash[:error].should == I18n.t(:slideshow_was_not_deleted) end 

这是products_controller.rb中的控制器操作:

 def destroy product = Product.find_by_id(params[:id]) redirect_to "profile" if !product if product.destroy flash[:notice] = t(:slideshow_was_deleted) if current_user.admin? and product.user != current_user redirect_to :products, :notice => t(:slideshow_was_deleted) else redirect_to "/profile" end else if current_user.admin? redirect_to :products, :error => t(:slideshow_was_not_deleted) else redirect_to "/profile" end end end 

现在,我没想到规格第一次通过,但我不明白为什么它失败了:

 Failure/Error: delete :destroy, {:id => 9999} ActiveRecord::RecordNotFound: Couldn't find Product with id=9999 

我的印象是#find_by_id不会在不存在的记录上返回RecordNotFound错误。 那我为什么要买一个呢? 提前致谢!

CanCan抛出了RecordNotFound错误。 它不能在控制器动作中拯救(可能是在动作运行之前发生)。 有两种方法可以解决它 –

  1. 将规格更改为:

     it "deleting a non-existent product should result in a RecordNotFound Error" do product_id = 9999 expect { delete :destroy, {:id => product_id}}.to raise_error ActiveRecord::RecordNotFound end 

或者,2。修补CanCan 就像这样 。

我不喜欢修补路线,所以我去了选项1。