Sha256: e721a4836cc73ed13ade067d87e2d5517e0a293114c7094ec7dfdcd9626711c0

Contents?: true

Size: 1.44 KB

Versions: 8

Compression:

Stored size: 1.44 KB

Contents

module RSpecCandy
  module Helpers
    module ShouldReceiveAndExecute

      def should_receive_and_execute(method)

        method = method.to_s
        method_base = method.gsub(/([\?\!\=\[\]]+)$/, '')
        method_suffix = $1

        method_called = "_#{method_base}_called#{method_suffix}"
        method_with_spy = "#{method_base}_with_spy#{method_suffix}"
        method_without_spy = "#{method_base}_without_spy#{method_suffix}"

        prototype = respond_to?(:singleton_class) ? singleton_class : metaclass
        prototype.class_eval do

          method_defined_directly = method_defined?(method) || private_method_defined?(method) # check that a method is not "defined" by responding to method_missing

          unless method_defined?(method_called)
            define_method method_called do |*args|
            end
          end

          if method_defined_directly
            unless method_defined?(method_with_spy)
              define_method method_with_spy do |*args, &block|
                send(method_called, *args)
                send(method_without_spy, *args, &block)
              end
              alias_method_chain method, :spy
            end
          else
            define_method method do |*args, &block|
              send(method_called, *args)
              super(*args, &block)
            end
          end

        end

        should_receive(method_called)
      end

      Object.send(:include, self)

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rspec_candy-0.5.1 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.5.0 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.4.1 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.4.0 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.3.1 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.3.0 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.2.10 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.2.9 lib/rspec_candy/helpers/should_receive_and_execute.rb