2

I'm looking at the testing docs for Savon here and i don't understand what's going on. I'm fairly new to testing with mocks and stubbing and maybe that's the issue. Here is the example:

require "spec_helper"

# require the helper module
require "savon/mock/spec_helper"

describe AuthenticationService do
  # include the helper module
  include Savon::SpecHelper

  # set Savon in and out of mock mode
  before(:all) { savon.mock!   }
  after(:all)  { savon.unmock! }

  describe "#authenticate" do
    it "authenticates the user with the service" do
      message = { username: "luke", password: "secret" }
      fixture = File.read("spec/fixtures/authentication_service/authenticate.xml")

      # set up an expectation
      savon.expects(:authenticate).with(message: message).returns(fixture)

      # call the service
      service = AuthenticationService.new
      response = service.authenticate(message)

      expect(response).to be_successful
    end
  end
end

I understand that we set up an expectation with the fixture i.e. what the response should be.

We then call the service and get a response. My questions are: 1. Is a real call being made? 2. Is this response a real response?? 3. Can someone try to explain this overall for me please?

Cheers

Robbo
  • 1,292
  • 2
  • 18
  • 41

1 Answers1

1

No remote request would be made. Since you have mocked authenticate, the response will be short-circuited to your designated value. However, some other preliminary requests might be expected to happen first, like a GET for the WSDL.

Joe Atzberger
  • 3,079
  • 1
  • 18
  • 16
  • I am currently working on a very similar project, could you elaborate more on your answer please @Joe Atzberger – chrisgeeq Sep 01 '18 at 09:11
  • `savon.expects(:authenticate)...` is like an rspec `expect` statement. Therefore, savon will not actually authenticate remotely. `savon.mock!` means it won't connect for anything. The page linked to in OP (where this example comes from) goes into greater detail: http://savonrb.com/version2/testing.html – Joe Atzberger Sep 13 '18 at 23:02
  • Alright,but do you think this a better alternative to using webmock or vcr gem? – chrisgeeq Sep 14 '18 at 10:02
  • We used it in combination with VCR. – Joe Atzberger Sep 19 '18 at 22:15