-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsafe_deferrable_behaviour.rb
More file actions
75 lines (67 loc) · 2.19 KB
/
safe_deferrable_behaviour.rb
File metadata and controls
75 lines (67 loc) · 2.19 KB
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
# encoding: utf-8
shared_examples 'a safe Deferrable' do
let(:logger) { instance_double('Logger') }
let(:arguments) { [random_str] }
let(:errback_calls) { [] }
let(:success_calls) { [] }
let(:exception) { StandardError.new("Intentional error") }
before do
allow(subject).to receive(:logger).and_return(logger)
end
context '#errback' do
it 'adds a callback that is called when #fail is called' do
subject.errback do |*args|
expect(args).to eql(arguments)
end
subject.fail(*arguments)
end
it 'catches exceptions in the callback and logs the error to the logger' do
expect(subject.send(:logger)).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/#{exception.message}/)
end
subject.errback do
raise exception
end
subject.fail
end
end
context '#fail' do
it 'calls the callbacks defined with #errback, but not the ones added for success #callback' do
3.times do
subject.errback { errback_calls << true }
subject.callback { success_calls << true }
end
subject.fail(*arguments)
expect(errback_calls.count).to eql(3)
expect(success_calls.count).to eql(0)
end
end
context '#callback' do
it 'adds a callback that is called when #succed is called' do
subject.callback do |*args|
expect(args).to eql(arguments)
end
subject.succeed(*arguments)
end
it 'catches exceptions in the callback and logs the error to the logger' do
expect(subject.send(:logger)).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/#{exception.message}/)
end
subject.callback do
raise exception
end
subject.succeed
end
end
context '#succeed' do
it 'calls the callbacks defined with #callback, but not the ones added for #errback' do
3.times do
subject.errback { errback_calls << true }
subject.callback { success_calls << true }
end
subject.succeed(*arguments)
expect(success_calls.count).to eql(3)
expect(errback_calls.count).to eql(0)
end
end
end