main
1module Spank
2 describe Proxy do
3 subject { Proxy.new(target) }
4 let(:target) { double("target", :greet => nil) }
5
6 context "when invoking a method" do
7 before { subject.greet("blah") }
8
9 it "sends the message to the target" do
10 expect(target).to have_received(:greet).with("blah")
11 end
12 end
13
14 context "when an interceptor is registered" do
15 context "when invoking a method" do
16 let(:interceptor) { double("interceptor", intercept: "") }
17
18 before :each do
19 subject.add_interceptor(:greet, interceptor)
20 subject.greet("blah")
21 end
22 it "allows the interceptor to intercept the call" do
23 expect(interceptor).to have_received(:intercept)
24 end
25 end
26
27 context "when invoking a method with a block" do
28 it "passes the block to the target" do
29 proxy = Proxy.new([])
30 expect do
31 proxy.each do |x|
32 raise StandardError
33 end
34 end.to raise_error(StandardError)
35 end
36 end
37 end
38
39 context "when invoking a method that is not defined on the target" do
40 it "raises an error" do
41 expect { Proxy.new("blah").goodbye }.to raise_error(NoMethodError)
42 end
43 end
44 end
45end