首页 > 解决方案 > Rails 测试帖子内容

问题描述

导轨 6

如何在 Rails 测试中发布标题和正文?

考虑这样的小费

Rails 测试/集成

require 'test_helper'

class DataTest < ActionDispatch::IntegrationTest

  test "Post Profiles" do
    post "/candy/user/profiles", {headers: {:authorization => @session[:authorization]}}, {'RAW_POST_DATA' => 'something'}
  end

end

结果

DataTest#test_Post_Profiles:
ArgumentError: wrong number of arguments (given 3, expected 1)
    test/integration/candy/data_test.rb:31:in `block in <class:DataTest>'

这也无济于事 ,无法在控制器中看到任何发送的request.raw_post主体request.body.read

def raw_post(action, params, body)
    @request.env['RAW_POST_DATA'] = body
    response = post(action, params)
    @request.env.delete('RAW_POST_DATA')
    response
end

标签: ruby-on-railstdd

解决方案


原始帖子是请求正文,在现代 Rails 中,您可以传递一个字符串作为params参数:

  post "/candy/user/profiles", headers: {authorization: @session[:authorization]}, params: 'something'

推荐阅读