2012-04-18 135 views
5

我试图从使用respond_to切换到Rails控制器的respond_with。一切都很顺利,除了测试控制器规格中的无效保存外。这里有一个例子:Rails的respond_with&Rspec控制器:测试不成功的更新

形容做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 

这工作只是我的旧的respond_to款式更新动作不错,但与respond_with,我得到

Failure/Error: 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 

回答

5

我一直在寻找这个确切的事情(我怎么发现这个话题) - 到目前为止,我有以下几点:

Location.any_instance.stub(:valid?).and_return(false) 
Location.any_instance.stub(:errors).and_return('anything') 

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

但是我BELI如果我找到它,我一定会发布它!

注意:我也使用响应者宝石,因此如果您不使用它,其中一行可能不是必需的!