使用 RSpec 测试 Rails 3 respond_with

发布于 2024-11-29 16:49:41 字数 1275 浏览 0 评论 0原文

我最近将我的控制器代码从: 更改

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-12-06 16:49:41

非常有趣,刚刚尝试过,实际上响应并不包含太多内容。

这只是一个状态 302。测试:response.status.should eq 302

正文如下:

"您正在 redirected.”

也很容易测试。

我会进一步挖掘。


编辑:

即使使用render_viewsresponse仍然只是一个重定向。

您还可以检查response.header,如下所示:

{"Location"=>"http://test.host/users", "Content-Type"=>"text/html; charset=utf-8"}

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:

{"Location"=>"http://test.host/users", "Content-Type"=>"text/html; charset=utf-8"}
jJeQQOZ5 2024-12-06 16:49:41

原因是responds_with检查.errors.empty?而不是valid?(我想它不想不必要地重新运行验证)。所以我猜想去掉 .errors

The reason is that responds_with checks .errors.empty? as opposed to valid? (I suppose it doesn't want to unnecessarily re-run validations). So stub out .errors I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文