Class: Object
- Inherits:
- BasicObject
- Includes:
- RSpec::Mocks::ExampleMethods
- Defined in:
- lib/rspecproxies/proxies.rb
Instance Method Summary collapse
-
#capture_result_from(target, method_name, details) ⇒ Object
Will capture (or override) result from the target’s method in the details instance variable.
-
#capture_results_from(method_name) ⇒ Object
Will capture all the results of the method into the returned array.
-
#on_call_to(method_name) ⇒ Object
Will call the given block with all the actual arguments every time the method is called.
-
#on_result_from(method_name) ⇒ Object
Will call the given block with it’s result every time the method returns.
-
#proxy_chain(*selectors, &block) ⇒ Object
Sets up proxies all down the selector chain, until the last where a simple method stub is created and on which the block is called.
Instance Method Details
#capture_result_from(target, method_name, details) ⇒ Object
Will capture (or override) result from the target’s method in the details instance variable
29 30 31 32 |
# File 'lib/rspecproxies/proxies.rb', line 29 def capture_result_from(target, method_name, details) into = details[:into] target.on_result_from(method_name) {|result| instance_variable_set("@#{into}", result)} end |
#capture_results_from(method_name) ⇒ Object
Will capture all the results of the method into the returned array
21 22 23 24 25 |
# File 'lib/rspecproxies/proxies.rb', line 21 def capture_results_from(method_name) all_results = [] on_result_from(method_name) {|result| all_results << result } all_results end |
#on_call_to(method_name) ⇒ Object
Will call the given block with all the actual arguments every time the method is called
36 37 38 39 40 41 42 43 44 |
# File 'lib/rspecproxies/proxies.rb', line 36 def on_call_to(method_name) stock_response = self.original_response(method_name) allow(self).to receive(method_name) do |*args, &block| yield *args stock_response.call(*args, &block) end end |
#on_result_from(method_name) ⇒ Object
Will call the given block with it’s result every time the method returns
9 10 11 12 13 14 15 16 17 |
# File 'lib/rspecproxies/proxies.rb', line 9 def on_result_from(method_name) stock_response = self.original_response(method_name) allow(self).to receive(method_name) do |*args, &block| result = stock_response.call(*args, &block) yield result result end end |
#proxy_chain(*selectors, &block) ⇒ Object
Sets up proxies all down the selector chain, until the last where a simple method stub is created and on which the block is called. Allows to set up stub chains quickly and safely.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rspecproxies/proxies.rb', line 49 def proxy_chain(*selectors, &block) if selectors.count == 1 final_stub = allow(self).to receive(selectors.first) block.call(final_stub) unless block.nil? else self.on_result_from(selectors.first) do |result| result.proxy_chain(*selectors[1..-1], &block) end end end |