module RSpec::Intercept
Constants
- VERSION
Public Instance Methods
intercept(klass, method_name, &block)
click to toggle source
Public: Intercept
a class method call
Stubs the class method ‘method_name` of `klass` and passes a method object and the arguments into the block for the user to temper with. The user has to call the method object with the arguments themselves.
Examples
intercept(Foo, :bar) do |method, *args| # You could temper with the arguments here # Don't forget to call the original method. result = method.call(*args) # You could temper with the result here result end
# File lib/rspec/intercept.rb, line 26 def intercept(klass, method_name, &block) klass.singleton_class. send(:alias_method, :"#{method_name}_original", method_name) allow(klass).to receive(method_name) do |*args, &received_block| block.call(klass.method(:"#{method_name}_original"), *args, &received_block) end end
intercept_any_instance_of(klass, method_name, &block)
click to toggle source
Public: Intercept
an instance method call
Stubs the instance method ‘method_name` of `klass` and passes a method object and the arguments into the block for the user to temper with. The user has to call the method object with the arguments themselves.
Examples
intercept_any_instance_of(Foo, :bar) do |method, *args| # You could temper with the arguments here # Don't forget to call the original method. result = method.call(*args) # You could temper with the result here result end
# File lib/rspec/intercept.rb, line 54 def intercept_any_instance_of(klass, method_name, &block) klass.send(:alias_method, :"#{method_name}_original", method_name) allow_any_instance_of(klass).to receive(method_name) do |instance, *args, &received_block| block.call(instance.method(:"#{method_name}_original"), *args, &received_block) end end