最近,我改变了我的控制器代码:测试的Rails 3 respond_with使用RSpec
def create
@checklist_item = @checklist.items.build(params[:checklist_item])
if @checklist_item.save
flash[:notice] = "Successfully created checklist item."
redirect_to checklist_item_url(@checklist, @checklist_item)
else
render :action => 'new'
end
end
到
respond_to :html, :json
def create
@checklist_item = @checklist.items.build(params[:checklist_item])
if @checklist_item.save
flash[:notice] = "Successfully created checklist item."
end
respond_with @checklist_item
end
但我的天赋,与我以前的控制器代码工作正常失败:
it "create action should render new template when model is invalid" do
checklist_item.stub(:valid? => false)
checklist.stub_chain(:items, :build => checklist_item)
post :create, :checklist_id => checklist.id
response.should render_template(:new)
end
随着错误:
1) Checklists::ItemsController create action should render new template when model is invalid
Failure/Error: response.should render_template(:new)
MiniTest::Assertion:
Expected block to return true value.
我不知道如何更改规格。当我在浏览器中测试它时,所有功能仍然保持不变(其渲染为新的)。
奇怪它不呈现显示。你确定它会呈现新的? – apneadiving
目标是渲染:新模型是无效的。其他测试通过一个有效的模型可以正常工作并重定向以显示没有问题。 – dMix