Sha256: fac308a78b0f295da9c2c1c0f7a617ea40ad3e4e5341f5a0bc622c36567b1f7e

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Bogus
  class VerifiesStubDefinition
    extend Takes

    takes :method_stringifier

    def verify!(object, method_name, args)
      stubbing_non_existent_method!(object, method_name) unless object.respond_to?(method_name)
      return unless object.methods.include?(method_name)
      return if any_args?(args)
      method = object.method(method_name)
      verify_call!(method, args)
    end

    private

    def verify_call!(method, args)
      object = Object.new
      fake_method = method_stringifier.stringify(method, "")
      object.instance_eval(fake_method)
      object.send(method.name, *args)
    rescue ArgumentError
      wrong_arguments!(method, args)
    end

    def wrong_arguments!(method, args)
      args_string = method_stringifier.arguments_as_string(method.parameters)
      raise ArgumentError, "tried to stub #{method.name}(#{args_string}) with arguments: #{args.map(&:inspect).join(",")}"
    end

    def stubbing_non_existent_method!(object, method_name)
      raise NameError, "#{object.inspect} does not respond to #{method_name}"
    end

    def any_args?(args)
      [Bogus::AnyArgs] == args
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bogus-0.1.0 lib/bogus/verifies_stub_definition.rb