File: verifies_stub_definition.rb

package info (click to toggle)
ruby-bogus 0.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 828 kB
  • ctags: 628
  • sloc: ruby: 4,124; makefile: 6; sh: 2
file content (35 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 WithArguments.with_matcher?(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
  end
end