lib/spy/subroutine.rb in spy-0.2.4 vs lib/spy/subroutine.rb in spy-0.2.5
- old
+ new
@@ -38,11 +38,11 @@
# @param [Hash] opts what do do when hooking into a method
# @option opts [Boolean] force (false) if set to true will hook the method even if it doesn't exist
# @option opts [Symbol<:public, :protected, :private>] visibility overrides visibility with whatever method is given
# @return [self]
def hook(opts = {})
- raise "#{base_object} method '#{method_name}' has already been hooked" if hooked?
+ raise AlreadyHookedError, "#{base_object} method '#{method_name}' has already been hooked" if self.class.get(base_object, method_name, singleton_method)
@hook_opts = opts
@original_method_visibility = method_visibility_of(method_name)
hook_opts[:visibility] ||= original_method_visibility
hook_opts[:force] ||= base_object.is_a?(Double)
@@ -64,11 +64,11 @@
end
# unhooks method from object
# @return [self]
def unhook
- raise "'#{method_name}' method has not been hooked" unless hooked?
+ raise NeverHookedError, "'#{method_name}' method has not been hooked" unless hooked?
if original_method && method_owner == original_method.owner
method_owner.send(:define_method, method_name, original_method)
method_owner.send(original_method_visibility, method_name) if original_method_visibility
else
@@ -109,11 +109,11 @@
if block_given?
if value.is_a?(Hash) && value.has_key?(:force)
@do_not_check_plan_arity = !!value[:force]
elsif !value.nil?
- raise ArgumentError.new("value and block conflict. Choose one")
+ raise ArgumentError, "value and block conflict. Choose one"
end
@plan = Proc.new
check_for_too_many_arguments!(@plan)
else
@@ -182,19 +182,19 @@
end
# if the method was called it will return true
# @return [Boolean]
def has_been_called?
- raise "was never hooked" unless @was_hooked
+ raise NeverHookedError unless @was_hooked
calls.size > 0
end
# check if the method was called with the exact arguments
# @param args Arguments that should have been sent to the method
# @return [Boolean]
def has_been_called_with?(*args)
- raise "was never hooked" unless @was_hooked
+ raise NeverHookedError unless @was_hooked
calls.any? do |call_log|
call_log.args == args
end
end
@@ -303,10 +303,26 @@
def method_owner
@method_owner ||= current_method.owner
end
class << self
- # retrieve the method spy from an object
+
+ # retrieve the method spy from an object or create a new one
+ # @param base_object
+ # @param method_name [Symbol]
+ # @param singleton_method [Boolean] this a singleton method or a instance method?
+ # @return [Array<Subroutine>]
+ def on(base_object, method_name, singleton_method = true)
+ new(base_object, method_name, singleton_method).hook
+ end
+
+ def off(base_object, method_name, singleton_method = true)
+ spy = get(base_object, method_name, singleton_method = true)
+ raise NoSpyError, "#{method_name} was not spied on #{base_object}" unless spy
+ spy.unhook
+ end
+
+ # retrieve the method spy from an object or return nil
# @param base_object
# @param method_name [Symbol]
# @param singleton_method [Boolean] this a singleton method or a instance method?
# @return [Array<Subroutine>]
def get(base_object, method_name, singleton_method = true)