Question Details

No question body available.

Tags

ruby-on-rails minitest

Answers (2)

March 12, 2026 Score: 1 Rep: 6,465 Quality: Low Completeness: 40%

each condition can be tested via a stub

stubrequest(:post, "https://www.example.co/ae/api.php"). toraise(Errno::ECONNREFUSED)

and

stubrequest(:post, "https://www.example.co/ae/api.php"). totimeout
March 12, 2026 Score: -1 Rep: 438 Quality: Low Completeness: 50%
it "handles a timeout from the external API" do
  allow(HTTParty).to receive(:post).andraise(Timeout::Error)

post confirm
executionurl, params: { txdocid: thistxdoc.id }, headers: { "host" => "www.example.com" }

expect(flash[:alert]).to eq("API connection failed. Retrying in background...") # or whatever else you assert on — redirect, response status, DB state, etc. end

You can do the same for the other rescued exceptions:

[Timeout::Error, Errno::ECONNREFUSED, SocketError].each do |errorclass|
  it "handles #{errorclass} from the external API" do
    allow(HTTParty).to receive(:post).andraise(errorclass)

post confirm
executionurl, params: { txdocid: this_txdoc.id }, headers: { "host" => "www.example.com" }

expect(flash[:alert]).to eq("API connection failed. Retrying in background...") end end