2012-02-08 57 views
7

我用Rails 3.1.3与继承的资源1.3.0项目。的Rails 3.1.3和继承的资源测试失败

当我有像这样的控制器:

class PostsController < InheritedResources::Base 
end 

我与RSpec的测试以下

describe "PUT update" do 
    describe "with invalid params" do 
     it "re-renders the 'edit' template" do 
     post = Post.create! valid_attributes 
     # Trigger the behavior that occurs when invalid params are submitted 
     Post.any_instance.stub(:save).and_return(false) 
     put :update, {:id => post.to_param, :post => {}}, valid_session 
     response.should render_template("edit") 
     end 
    end 
    end 

我收到以下错误:

3) PostsController PUT update with invalid params re-renders the 'edit' template 
    Failure/Error: response.should render_template("edit") 
     expecting <"edit"> but rendering with <""> 
    # ./spec/controllers/posts_controller_spec.rb:115:in `block (4 levels) in <top (required)>' 

这是为什么?我需要存储其他东西吗?

回答

13

只需补充一点:

Post.any_instance.stub(:errors).and_return(['error']) 

右后:

Post.any_instance.stub(:save).and_return(false)