/
before_spec.rb
85 lines (72 loc) · 2.26 KB
/
before_spec.rb
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
require 'spec_helper'
describe Typhoeus::Hydra::Before do
let(:request) { Typhoeus::Request.new("") }
let(:hydra) { Typhoeus::Hydra.new }
after { Typhoeus.before.clear }
describe "#add" do
context "when before" do
context "when one" do
it "executes" do
Typhoeus.before { |r| String.new(r.url) }
String.should_receive(:new).and_return("")
hydra.add(request)
end
context "when true" do
it "calls super" do
Typhoeus.before { true }
Typhoeus::Expectation.should_receive(:find_by)
hydra.add(request)
end
end
context "when false" do
it "doesn't call super" do
Typhoeus.before { false }
Typhoeus::Expectation.should_receive(:find_by).never
hydra.add(request)
end
end
context "when response" do
it "doesn't call super" do
Typhoeus.before { Typhoeus::Response.new }
Typhoeus::Expectation.should_receive(:find_by).never
hydra.add(request)
end
end
end
context "when multi" do
context "when all true" do
before { 3.times { Typhoeus.before { |r| String.new(r.url) } } }
it "calls super" do
Typhoeus::Expectation.should_receive(:find_by)
hydra.add(request)
end
it "executes all" do
String.should_receive(:new).exactly(3).times.and_return("")
hydra.add(request)
end
end
context "when middle false" do
before do
Typhoeus.before { |r| String.new(r.url) }
Typhoeus.before { |r| String.new(r.url); nil }
Typhoeus.before { |r| String.new(r.url) }
end
it "doesn't call super" do
Typhoeus::Expectation.should_receive(:find_by).never
hydra.add(request)
end
it "executes only two" do
String.should_receive(:new).exactly(2).times.and_return("")
hydra.add(request)
end
end
end
end
context "when no before" do
it "calls super" do
Typhoeus::Expectation.should_receive(:find_by)
hydra.add(request)
end
end
end
end