File: verifies_stub_definition_spec.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 (98 lines) | stat: -rw-r--r-- 2,450 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'spec_helper'

describe Bogus::VerifiesStubDefinition do
  class ExampleForVerify
    def foo(bar)
    end

    def three_args(x, y, z)
    end

    def optional_args(x, y = 1, z = 2)
    end

    def var_args(x, *y)
    end
  end

  let(:object) { ExampleForVerify.new }
  let(:verifies_stub_definition) { Bogus::VerifiesStubDefinition.new(method_stringifier) }
  let(:method_stringifier) { isolate(Bogus::MethodStringifier) }

  def verify(method_name, args)
    verifies_stub_definition.verify!(object, method_name, args)
  end

  def it_allows(method_name, args)
    expect{ verify(method_name, args) }.not_to raise_error
  end

  def it_disallows(method_name, args, error = ArgumentError)
    expect{ verify(method_name, args) }.to raise_error(error)
  end

  def self.it_allows_argument_numbers(method_name, *arg_counts)
    arg_counts.each do |arg_count|
      it "allows #{arg_count} arguments" do
        it_allows(method_name, [1] * arg_count)
      end
    end
  end

  def self.it_disallows_argument_numbers(method_name, *arg_counts)
    arg_counts.each do |arg_count|
      it "disallows #{arg_count} arguments" do
        it_disallows(method_name, [1] * arg_count)
      end
    end
  end

  it "checks for method presence" do
    it_disallows(:bar, [1], NameError)
  end

  it "allows interactions that use any_args" do
    it_allows(:three_args, [Bogus::AnyArgs])
  end

  describe "method arity checks" do
    context "method with positive arity" do
      it_allows_argument_numbers :three_args, 3
      it_disallows_argument_numbers :three_args, 2, 4
    end

    context "method with optional arguments" do
      it_allows_argument_numbers :optional_args, 1, 2, 3
      it_disallows_argument_numbers :optional_args, 0, 4
    end

    context "method with infinite number of arguments" do
      it_allows_argument_numbers :var_args, 1000
      it_disallows_argument_numbers :var_args, 0
    end
  end

  class UsesMethodMissing
    def respond_to?(method)
      method == :foo
    end

    def method_missing(name, *args, &block)
      return super unless name == :foo
      :bar
    end
  end

  context "with objects that use method missing" do
    let(:object) { UsesMethodMissing.new }

    it "allows stubbing methods that the object responds to" do
      it_allows(:foo, [])
    end

    it "disallows stubbing methods that the object does not respond to" do
      it_disallows(:bar, [], NameError)
    end
  end
end