File: hooks_spec.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (158 lines) | stat: -rw-r--r-- 4,607 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'spec_helper'

describe VCR::Hooks::FilteredHook do
  describe "#conditionally_invoke" do
    it 'invokes the hook' do
      called = false
      subject.hook = lambda { called = true }
      subject.conditionally_invoke
      expect(called).to be true
    end

    it 'forwards the given arguments to the hook' do
      args = nil
      subject.hook = lambda { |a, b| args = [a, b] }
      subject.conditionally_invoke(3, 5)
      expect(args).to eq([3, 5])
    end

    it 'forwards only as many arguments as the hook block accepts' do
      args = nil
      subject.hook = lambda { |a| args = [a] }
      subject.conditionally_invoke(3, 5)
      expect(args).to eq([3])
    end

    it 'does not invoke the hook if all of the filters return false' do
      called = false
      subject.hook = lambda { called = true }
      subject.filters = lambda { false }
      subject.conditionally_invoke
      expect(called).to be false
    end

    it 'does not invoke the hook if any of the filters returns false' do
      called = false
      subject.hook = lambda { called = true }
      subject.filters = [lambda { false }, lambda { true }]
      subject.conditionally_invoke
      expect(called).to be false
    end

    it 'forwards arguments to the filters' do
      filter_args = nil
      subject.filters = lambda { |a, b| filter_args = [a, b]; false }
      subject.conditionally_invoke(3, 5)
      expect(filter_args).to eq([3, 5])
    end

    it 'handles splat args properly' do
      filter_args = nil
      subject.filters = lambda { |*args| filter_args = args; false }
      subject.conditionally_invoke(3, 5)
      expect(filter_args).to eq([3, 5])
    end

    it 'forwards only as many arguments as the filter blocks accept' do
      args1 = args2 = nil
      subject.filters = [
        lambda { |a| args1 = [a]; true },
        lambda { |a, b| args2 = [a, b]; false }
      ]

      subject.conditionally_invoke(3, 5)
      expect(args1).to eq([3])
      expect(args2).to eq([3, 5])
    end

    it '#to_procs the filter objects' do
      filter_called = false
      subject.hook = lambda { }
      subject.filters = [double(:to_proc => lambda { filter_called = true })]
      subject.conditionally_invoke
      expect(filter_called).to be true
    end
  end
end

describe VCR::Hooks do
  let(:hooks_class) { Class.new { include VCR::Hooks } }

  subject { hooks_class.new }
  let(:invocations) { [] }

  before(:each) do
    hooks_class.instance_eval do
      define_hook :before_foo
      define_hook :before_bar, :prepend
    end
  end

  it 'allows the class to override the hook method and super to the main definition' do
    override_called = nil

    hooks_class.class_eval do
      define_method :before_foo do |&block|
        override_called = true
        super(&block)
      end
    end

    subject.before_foo { }
    expect(override_called).to be true
  end

  describe '#clear_hooks' do
    it 'clears all hooks' do
      subject.before_foo { invocations << :callback }
      subject.clear_hooks
      subject.invoke_hook(:before_foo)
      expect(invocations).to be_empty
    end
  end

  describe '#invoke_hook' do
    it 'invokes each of the callbacks' do
      subject.before_foo { invocations << :callback_1 }
      subject.before_foo { invocations << :callback_2 }

      expect(invocations).to be_empty
      subject.invoke_hook(:before_foo)
      expect(invocations).to eq([:callback_1, :callback_2])
    end

    it 'maps the return value of each callback' do
      subject.before_foo { 17 }
      subject.before_foo { 12 }
      expect(subject.invoke_hook(:before_foo)).to eq([17, 12])
    end

    it 'does not invoke any filtered callbacks' do
      subject.before_foo(:real?) { invocations << :blue_callback }
      subject.invoke_hook(:before_foo, double(:real? => false))
      expect(invocations).to be_empty
    end

    it 'invokes them in reverse order if the hook was defined with :prepend' do
      subject.before_bar { 17 }
      subject.before_bar { 12 }
      expect(subject.invoke_hook(:before_bar)).to eq([12, 17])
    end
  end

  describe "#has_hooks_for?" do
    it 'returns false when given an unrecognized hook name' do
      expect(subject).not_to have_hooks_for(:abcd)
    end

    it 'returns false when given the name of a defined hook that has no registered callbacks' do
      expect(subject).not_to have_hooks_for(:before_foo)
    end

    it 'returns true when given the name of a defined hook that has registered callbacks' do
      subject.before_foo { }
      expect(subject).to have_hooks_for(:before_foo)
    end
  end
end