【发布时间】:2010-01-20 18:42:30
【问题描述】:
我希望将原始帖子数据(例如未参数化的 JSON)发送到我的一个控制器进行测试:
class LegacyOrderUpdateControllerTest < ActionController::TestCase
test "sending json" do
post :index, '{"foo":"bar", "bool":true}'
end
end
但这给了我一个NoMethodError: undefined method `symbolize_keys' for #<String:0x00000102cb6080> 错误。
在ActionController::TestCase 中发送原始帖子数据的正确方法是什么?
这是一些控制器代码:
def index
post_data = request.body.read
req = JSON.parse(post_data)
end
【问题讨论】:
-
我也很好奇您是如何做到这一点的,以测试基于 JSON 的 API。
-
@Brian 标记为最佳接受答案的答案不再正确。你能更新这个吗?您可能应该选择一个使用
body参数的答案。这是在 Rails 5 和 Rails 6 中最好的方法。
标签: ruby-on-rails json testing