使用 RSpec 测试 Rails 3 respond_with
我最近将我的控制器代码从: 更改
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.
我不确定如何更改规范。当我在浏览器中测试它时,一切功能仍然相同(它呈现新的)。
I recently changed my controller code from:
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
to
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
But my spec that worked fine with my previous controller code is failing:
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
With the error:
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.
I'm not sure how to change the spec. Everything still functions the same when I test it in the browser (its renders new).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常有趣,刚刚尝试过,实际上
响应
并不包含太多内容。这只是一个状态 302。测试:
response.status.should eq 302
正文如下:
"您正在 redirected.”
也很容易测试。
我会进一步挖掘。
编辑:
即使使用
render_views
,response
仍然只是一个重定向。您还可以检查response.header,如下所示:
Pretty funny, just tried and indeed the
response
doesn't contain much.It's a mere status 302. Test:
response.status.should eq 302
With a body like:
"<html><body>You are being <a href=\"new_url">redirected</a>.</body></html>"
which is easily testable too.
I'll dig a little further.
Edit:
Even with
render_views
,response
remains a mere redirect.You could also check
response.header
which looks like:原因是responds_with检查
.errors.empty?
而不是valid?
(我想它不想不必要地重新运行验证)。所以我猜想去掉.errors
。The reason is that responds_with checks
.errors.empty?
as opposed tovalid?
(I suppose it doesn't want to unnecessarily re-run validations). So stub out.errors
I guess.