ruby-on-rails - 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
解决方案
原始帖子是请求正文,在现代 Rails 中,您可以传递一个字符串作为params
参数:
post "/candy/user/profiles", headers: {authorization: @session[:authorization]}, params: 'something'
推荐阅读
- gridview - Yii2:加载 index.php 时如何不填充 GridView
- angular - 存储更改时 NgRx 选择器不更新
- java - Spring Boot多模块属性不起作用
- r - 在数据框中将 ENSEMBL ID 转换为基因 ID
- python - 如何因为按键被按下而退出功能?
- angular - 值更改后未定义的角度可观察值
- php - PHP 从 SQL 中提取,然后选择特定的 ID
- python - 计算具有 n 个顶点的非自相交多边形的面积
- angular - Angular [(ngModel)] 在其他事件触发后更改数据
- open62541 - 使用 open62541 的基本时间序列(ADC)传输架构