Class: Savon::Spec::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/savon/spec/mock.rb

Overview

Savon::Spec::Mock

Mocks Savon SOAP requests.

Constant Summary collapse

HOOKS =

Hooks registered by Savon::Spec.

[:spec_action, :spec_body, :spec_response, :spec_never]

Instance Method Summary collapse

Instance Method Details

#expects(expected) ⇒ Object

Expects that a given action should be called.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/savon/spec/mock.rb', line 15

def expects(expected)
  self.action = expected

  Savon.config.hooks.define(:spec_action, :soap_request) do |_, request|
    actual = request.soap.input[1]
    raise ExpectationError, "expected #{action.inspect} to be called, got: #{actual.inspect}" unless actual == action

    respond_with
  end

  self
end

#neverObject

Expects that the action doesn’t get called.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/savon/spec/mock.rb', line 60

def never
  Savon.config.hooks.reject(:spec_action)

  Savon.config.hooks.define(:spec_never, :soap_request) do |_, request|
    actual = request.soap.input[1]
    raise ExpectationError, "expected #{action.inspect} never to be called, but it was!" if actual == action

    respond_with
  end

  self
end

#returns(response = nil) ⇒ Object

Expects a given response to be returned.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/savon/spec/mock.rb', line 46

def returns(response = nil)
  http = case response
    when Symbol then { :body => Fixture[action, response] }
    when Hash   then response
  end

  Savon.config.hooks.define(:spec_response, :soap_request) do |_, request|
    respond_with(http)
  end

  self
end

#with(body = nil, &block) ⇒ Object

Accepts a SOAP body to check if it was set. Also accepts a block which receives the Savon::SOAP::Request to set up custom expectations.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/savon/spec/mock.rb', line 30

def with(body = nil, &block)
  Savon.config.hooks.define(:spec_body, :soap_request) do |_, request|
    if block
      block.call(request)
    else
      actual = request.soap.body
      raise ExpectationError, "expected #{body.inspect} to be sent, got: #{actual.inspect}" unless actual == body
    end

    respond_with
  end

  self
end