【问题标题】:Rails 3.1.3 and Inherited Resources tests failRails 3.1.3 和继承资源测试失败
【发布时间】:2012-02-08 03:37:16
【问题描述】:

我将 Rails 3.1.3 用于具有继承资源 1.3.0 的项目。

当我有这样的控制器时:

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)>'

这是为什么?我必须把其他东西存起来吗?

【问题讨论】:

    标签: ruby-on-rails-3.1 inherited-resources


    【解决方案1】:

    只需添加这个:

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

    紧接着:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多