Sha256: 9216a729fe8db6247a25336690cc7df9906ed83e51570f32657fa6fb7c399fac

Contents?: true

Size: 994 Bytes

Versions: 3

Compression:

Stored size: 994 Bytes

Contents

module RSpecCandy
  module Helpers
    module ShouldReceiveAndExecute

      def should_receive_and_execute(method)
        method_base = method.to_s.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

          unless method_defined?(method_with_spy)

            define_method method_called do |*args|
            end

            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

        end

        should_receive(method_called)
      end

      Object.send(:include, self)

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec_candy-0.2.3 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.2.2 lib/rspec_candy/helpers/should_receive_and_execute.rb
rspec_candy-0.2.1 lib/rspec_candy/helpers/should_receive_and_execute.rb