Rails 测试:Net::HTTP post - 获取外部站点时 Rspec 中的空 response.body

问题描述 投票:0回答:0

奇怪的 rspec 行为:我从测试中发送了一个 post 请求。

context "when webmaster" do
    let(:question) { create(:question) }
    let(:user) { create(:user, :webmaster) }

    describe "creating question from admin form" do
      it "not success" do
        sign_in(user)
        region = "1c5b2444-70a0-4932-980c-b4dc0d3f02b5"
        question_params = { question: {
          phone: 123,
          region: region_id
        }}
        post :create_from_webmaster_modal, params: params
        expect(response).to have_http_status(:ok)
      end
    end
  end

它调用应用程序控制器(同一个应用程序)

def create_from_webmaster_modal
  ...
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    response = http.post(uri.path, params.to_json, headers)
    json_response = JSON.parse(response.body)
  ...
end

... 从外部 API 获取数据。 在所有情况下,控制器中的代码都从外部 API 获取数据。

在所有环境(生产、开发、测试)的控制台(rails c)中,一切正常。

在跑步的情况下

rspec

response.body 为空,响应为 HTTPOK

进入 http.post(...) 的所有参数都是正确的。

轨道 5.2 Ruby - 2.5(是的,这是一个旧应用程序(() Rspec-3.10

发生了什么事?

ruby-on-rails ruby testing rspec rspec-rails
© www.soinside.com 2019 - 2024. All rights reserved.