Rails respond_with和Rspec控制器:测试不成功的更新

Pla*_*Ton 5 rspec ruby-on-rails respond-with

我正试图在Rails控制器中使用respond_to切换到respond_with.一切顺利,除了测试控制器规格中的无效保存.这是一个例子:

描述MyController做...

describe "PUT update" do
    context "with invalid attributes" do
      it "should re-render the edit page" do
        style = stub_model(Style)
        Style.stub(:find) { style } 
        Style.any_instance.stub(:save).and_return(false)
        put :update
        response.should render_template(:edit)
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这与我的旧的respond_to样式更新操作一样正常,但是使用respond_with,我得到了

失败/错误:response.should render_template("edit")

所以,简而言之 - 我该如何测试?...或者我应该假设render_with知道它在做什么而不是测试?有什么一般建议?

提前干杯

PS:更新动作:

  def update
    @style = Style.find(params[:id])
    flash[:notice] = "Style updated" if @style.update_attributes(params[:style])
    respond_with(@style)
  end
Run Code Online (Sandbox Code Playgroud)

sev*_*cat 5

我一直在研究这个问题(我是如何找到这个主题的) - 到目前为止,我有以下内容:

Location.any_instance.stub(:valid?).and_return(false)
Location.any_instance.stub(:errors).and_return('anything')
Run Code Online (Sandbox Code Playgroud)

(其中Location是我使用respond_with的模型)

但我相信必须有一个更好的方法 - 如果我找到它,我一定会发布它!

注意:我也在使用响应者宝石,所以如果您不使用它,可能不需要其中一行!