mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-06 00:03:36 -04:00
Stubs in test mode can be called more than one time.
This commit is contained in:
parent
5bd3b864a6
commit
5918164275
@ -19,7 +19,7 @@ module Faraday
|
||||
class Stubs
|
||||
def initialize
|
||||
# {:get => [Stub, Stub]}
|
||||
@stack = {}
|
||||
@stack, @consumed = {}, {}
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
@ -29,8 +29,15 @@ module Faraday
|
||||
|
||||
def match(request_method, path, body)
|
||||
return false if !@stack.key?(request_method)
|
||||
stub = @stack[request_method].detect { |stub| stub.matches?(path, body) }
|
||||
@stack[request_method].delete stub
|
||||
stack = @stack[request_method]
|
||||
consumed = (@consumed[request_method] ||= [])
|
||||
|
||||
if stub = matches?(stack, path, body)
|
||||
consumed << stack.delete(stub)
|
||||
stub
|
||||
else
|
||||
matches?(consumed, path, body)
|
||||
end
|
||||
end
|
||||
|
||||
def get(path, &block)
|
||||
@ -53,10 +60,6 @@ module Faraday
|
||||
new_stub(:delete, path, &block)
|
||||
end
|
||||
|
||||
def new_stub(request_method, path, body=nil, &block)
|
||||
(@stack[request_method] ||= []) << Stub.new(path, body, block)
|
||||
end
|
||||
|
||||
# Raises an error if any of the stubbed calls have not been made.
|
||||
def verify_stubbed_calls
|
||||
failed_stubs = []
|
||||
@ -69,6 +72,16 @@ module Faraday
|
||||
end
|
||||
raise failed_stubs.join(" ") unless failed_stubs.size == 0
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def new_stub(request_method, path, body=nil, &block)
|
||||
(@stack[request_method] ||= []) << Stub.new(path, body, block)
|
||||
end
|
||||
|
||||
def matches?(stack, path, body)
|
||||
stack.detect { |stub| stub.matches?(path, body) }
|
||||
end
|
||||
end
|
||||
|
||||
class Stub < Struct.new(:path, :body, :block)
|
||||
|
@ -22,5 +22,16 @@ module Adapters
|
||||
def test_middleware_with_simple_path_sets_body
|
||||
assert_equal 'hello', @resp.body
|
||||
end
|
||||
|
||||
def test_middleware_can_be_called_several_times
|
||||
assert_equal 'hello', @conn.get("/hello").body
|
||||
end
|
||||
|
||||
def test_middleware_allow_different_outcomes_for_the_same_request
|
||||
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
||||
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
||||
assert_equal 'hello', @conn.get("/hello").body
|
||||
assert_equal 'world', @conn.get("/hello").body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user