forked from activemerchant/active_merchant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomm_stub.rb
39 lines (34 loc) · 884 Bytes
/
comm_stub.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module CommStub
class Stub
def initialize(gateway, action)
@gateway = gateway
@action = action
@complete = false
end
def check_request(&block)
@check = block
self
end
def respond_with(*responses)
@complete = true
check = @check
(class << @gateway; self; end).send(:define_method, :ssl_post) do |*args|
check.call(*args) if check
(responses.size == 1 ? responses.last : responses.shift)
end
@action.call
end
def complete?
@complete
end
end
def stub_comms(gateway=@gateway, &action)
if @last_comm_stub
assert @last_comm_stub.complete?, "Tried to stub communications when there's a stub already in progress."
end
@last_comm_stub = Stub.new(gateway, action)
end
def teardown
assert(@last_comm_stub.complete?) if @last_comm_stub
end
end