在rspec中使用`puts response`进行堆栈级别太深

gin*_*ime 4 rspec rspec-rails ruby-on-rails-3

控制器测试中的rspec和rails有一个奇怪的问题.每当我们添加puts response一个规范时,它会输出很多这些

200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
Run Code Online (Sandbox Code Playgroud)

然后失败了SystemStackError: stack level too deep.通过pry检查响应效果很好,打印其他东西也很好.

升级到最新的rspec(2.11)并没有什么不同.我们注意到puts调用to_a响应,它返回一个数组[@status, @header, self],所以它会以某种方式导致这种奇怪的递归?

更新:这是代码+规范的要点

Myr*_*ton 5

我相信你遇到了机架中的错误. [rack_response].flatten进入无限循环.有关详细信息,请参阅这些问题

解决方案是不对响应对象本身设置任何期望,而是单独设置对状态,标题或正文的期望,例如:

last_response.status.should eq(200)
last_response.body.should include("some text")
last_response.headers.should include("Content-Type" => "text/plain")
Run Code Online (Sandbox Code Playgroud)