【问题标题】:Rspec rendering textRspec 渲染文本
【发布时间】:2012-06-06 05:20:41
【问题描述】:

我有这个代码

if @temp_user.save
  sign_in(:user, @temp_user)
  render text: "OK"
else
  render text: render_to_string(:partial => "errors")
end

我尝试使用 rspec 验证渲染“OK”

这是我的实际规格:

  it "render text OK" do   
    post :create, {:agent => valid_attributes}
    # response.should have_content("OK")
    response.should render_template(:text => "OK")
  end

但是这个规范总是响应 0 个失败,即使我把“OKI”放在“OK”的位置

有人对此有什么建议吗?

【问题讨论】:

  • 可能是'描述“渲染文本OK”做'而不是'它“渲染文本OK”做'?

标签: ruby-on-rails ruby-on-rails-3 rspec capybara rspec-rails


【解决方案1】:
response.body.should == "OK"

为我工作

【讨论】:

  • 在 rspec 3 及以上应为:expect(response.body).to eq "OK"
【解决方案2】:

如果您使用的是 rails 3 或更高版本

expect(response.body).to eq "OK"

会起作用

【讨论】:

    【解决方案3】:
    describe "render text OK" do   
      post :create, {:agent => valid_attributes}
      # response.should have_content("OK"
      response.should render_template(:text => "OK")
    end
    

    【讨论】:

    • expect(response).to render_template(:text => '')
    • @drhenner 对我的建议结果到Unknown key: :text. Valid keys are: :layout, :partial, :locals, :count, :file
    • 我唯一的目的是使用expect...你使用的是什么版本的Rails?
    • @drhenner 的答案来自 2012 年。RSpec 将其语法从 should 更改为 expect.rspec.info/blog/2012/06/rspecs-new-expectation-syntax
    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2014-07-13
    • 2011-08-11
    相关资源
    最近更新 更多