2

I'm trying to mock a SOAP request made with Savon. I can't seem to correctly mock the Savon response with my supplied fixture.

Versions:

savon (2.2.0)
wasabi (~> 3.1.0)

I had to lock the WSDL parser Wasabi to ‘3.1.0’ because of this: https://github.com/savonrb/wasabi/issues/61

Spec:

before(:all) do
  savon.mock!
end

before(:each) do
  wsdl = File.read("spec/fixtures/hours_import_success.xml")

  stub_request(:get, /.*/).to_return(:status => 200, :body => wsdl)
  request
end

it 'submits the hours' do
  json_response
  expect(response.status).to eq(200)
end

The json_response:

{"errors"=>{"base"=>["Could not call Soap service: Unable to find SOAP operation: :hours_import\nOperations provided by your service: []"]}}

My Soap integration with Savon

def submit_hours
  response = hours_import.body

  if response[:hours_import][:status] == '100'
    add(response: {
      status:  200,
      message: 'Submitted hours'
      })
  else
    log_error(response)
    add_errors({base: I18n.t('api.hours_import.errors.base')})
  end
end

def hours_import
  begin
    savon_client.call(
      :hours_import,
      message: {**credentials, message: hours_import_message}
    )
  rescue Savon::HTTPError, Savon::UnknownOperationError => e
    log_error(e)
    return terminate("Could not call Soap service: #{e}")
  end
end

def hours_import_message
  hours_import_body.to_xml(
    root: 'message'
  )
end

def savon_client
  Savon.client(wsdl: ENV['WSDL_URL'])
end

An interesting resource that tries to accomplish the same: https://github.com/savonrb/savon/issues/396

Kevin Etore
  • 1,046
  • 3
  • 14
  • 32
  • Any chance your `hours_import_success.xml` file is actually the success response and not the WSDL you need for this service? I believe your `stub_request` will block both the initial call your client makes for the WSDL _and_ the call for `hours_import`, meaning the response payload doesn't include an operations definition. – Alex Dec 15 '20 at 20:20

0 Answers0