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

查看:103
本文介绍了为什么在测试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天全站免登陆