2012-04-18 132 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如果我找到它,我一定會發布它!

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