如何在rails测试中为POST请求设置消息体?

时间:2016-01-25 05:07:14

标签: ruby-on-rails

我试图在我的集成测试中将POST主体设置为任意字符串。我的代码:

@request.env['RAW_POST_DATA'] = "testing 1 2 3"
post '/receiver'

当我这样做时,我应该在控制器中看到“测试1 2 3”:

foo = request.body.read

但“请求”收益率为零。有谁知道如何使rails集成测试通过“测试1 2 3”到原始消息体?

1 个答案:

答案 0 :(得分:0)

如果您正在进行集成测试,我会使用capybara gem来传递帖子请求。如果你有一个字段可以填写要发布的变量,你可以在capybara中指定,否则只要在控制器中传递强参数就可以传递任意变量。

例如,此代码使用Rspec和Capybara

发送帖子请求
def test_create_account_as(email, password)
  visit new_user_registration_path
  fill_in "user_email", with: email
  fill_in "user_password", with: password
  fill_in "user_password_confirmation", with: password
  click_button "Sign up"
end
相关问题