Why is JBuilder not returning a response body in JSON when testing RSPEC

限于喜欢 提交于 2019-11-30 16:23:28

问题


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.

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

----- Debugger

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

get :index, :format => :json
end

(rdb:1) response.body

"{}"


回答1:


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

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 :)




回答2:


You could find a solution from https://github.com/rails/jbuilder/issues/32

So in rspec/spec_helper.rb

RSpec.configure do |config|
  # https://github.com/rails/jbuilder/issues/32
  config.render_views = true
end

After above option are added, JSON result will show via jbuilder renderer



来源:https://stackoverflow.com/questions/9965945/why-is-jbuilder-not-returning-a-response-body-in-json-when-testing-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!