为什么在测试 RSPEC 时 JBuilder 不返回 JSON 格式的响应正文 [英] Why is JBuilder not returning a response body in JSON when testing RSPEC

查看:23
本文介绍了为什么在测试 RSPEC 时 JBuilder 不返回 JSON 格式的响应正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DHH 的 JBuilder 测试来自 RSPEC 控制器测试的 JSON 响应时,我的 response.body 始终为{}".它在开发/生产模式下工作正常,并且当我使用 to_json 方法而不是 jbuilder 时,我在我的 response.body 中得到了正确的 JSON.

When testing a JSON response from an RSPEC controller test using DHH's JBuilder, my response.body is always "{}". It works fine in development/production modes AND when I use the to_json method instead of jbuilder, I get proper JSON in my response.body.

有谁知道为什么我的 response.body 在测试时总是{}"?

Anyone have a clue as to why my response.body would always be "{}" when testing?

-----调试器

它应该通过 JSON 返回购物车项目",:focus do

it "should return the cart items via JSON", :focus do

获取 :index, :format => :json
结束

get :index, :format => :json
end

(rdb:1) response.body

(rdb:1) response.body

{}"

推荐答案

适用于遇到相同问题的任何人.我已经想通了.

For anyone that is having the same issue. I have figured it out.

您必须在正在执行的控制器测试中调用 render_views.如果你这样做,你应该会看到一个 response.body 包含你的 JSON :)

You must call render_views within the controller tests you are doing. If you do that, you should then see a response.body with your JSON contained :)

这篇关于为什么在测试 RSPEC 时 JBuilder 不返回 JSON 格式的响应正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆