Skip to content

Commit 069975d

Browse files
committed
Switch to RSpec 3 expect-based syntax
1 parent 81b44d0 commit 069975d

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ gem 'rake'
44
gem 'yard'
55

66
group :test do
7-
gem 'rspec', '>= 2.14'
7+
gem 'rspec', '~> 3.1'
88
end
99

1010
platforms :rbx do

lib/retryable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def retryable(options = {}, &block)
2222

2323
# Interrupt Exception could be raised while sleeping
2424
begin
25-
sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
25+
Kernel.sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
2626
rescue *on_exception
2727
end
2828

spec/lib/config_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require 'spec_helper'
22

3-
describe Retryable do
3+
RSpec.describe Retryable do
44
it 'is enabled by default' do
5-
Retryable.should be_enabled
5+
expect(Retryable).to be_enabled
66
end
77

88
it 'could be disabled' do
99
Retryable.disable
10-
Retryable.should_not be_enabled
10+
expect(Retryable).not_to be_enabled
1111
end
1212

1313
context 'when disabled' do
@@ -17,7 +17,7 @@
1717

1818
it 'could be re-enabled' do
1919
Retryable.enable
20-
Retryable.should be_enabled
20+
expect(Retryable).to be_enabled
2121
end
2222
end
2323
end

spec/lib/retryable_spec.rb

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,118 @@
11
require 'spec_helper'
22

3-
describe 'Kernel.retryable' do
3+
RSpec.describe 'Kernel.retryable' do
44
before(:each) do
55
Retryable.enable
66
@attempt = 0
77
end
88

99
it 'catch StandardError only by default' do
10-
lambda do
10+
expect do
1111
count_retryable(:tries => 2) { |tries, ex| raise Exception if tries < 1 }
12-
end.should raise_error Exception
13-
@try_count.should == 1
12+
end.to raise_error Exception
13+
expect(@try_count).to eq(1)
1414
end
1515

1616
it 'retries on default exception' do
17-
Kernel.should_receive(:sleep).once.with(1)
17+
expect(Kernel).to receive(:sleep).once.with(1)
1818

1919
count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 }
20-
@try_count.should == 2
20+
expect(@try_count).to eq(2)
2121
end
2222

2323
it 'does not retry if disabled' do
2424
Retryable.disable
2525

26-
lambda do
26+
expect do
2727
count_retryable(:tries => 2) { raise }
28-
end.should raise_error RuntimeError
29-
@try_count.should == 1
28+
end.to raise_error RuntimeError
29+
expect(@try_count).to eq(1)
3030
end
3131

3232
it 'executes *ensure* clause' do
3333
ensure_cb = Proc.new do |retries|
34-
retries.should == 0
34+
expect(retries).to eq(0)
3535
end
3636

3737
Kernel.retryable(:ensure => ensure_cb) { }
3838
end
3939

4040
it 'passes retry count and exception on retry' do
41-
Kernel.should_receive(:sleep).once.with(1)
41+
expect(Kernel).to receive(:sleep).once.with(1)
4242

4343
count_retryable(:tries => 2) do |tries, ex|
44-
ex.class.should == StandardError if tries > 0
44+
expect(ex.class).to eq(StandardError) if tries > 0
4545
raise StandardError if tries < 1
4646
end
47-
@try_count.should == 2
47+
expect(@try_count).to eq(2)
4848
end
4949

5050
it 'makes another try if exception is covered by :on' do
51-
Kernel.stub(:sleep)
51+
allow(Kernel).to receive(:sleep)
5252
count_retryable(:on => [StandardError, ArgumentError, RuntimeError] ) { |tries, ex| raise ArgumentError if tries < 1 }
53-
@try_count.should == 2
53+
expect(@try_count).to eq(2)
5454
end
5555

5656
it 'does not try on unexpected exception' do
57-
Kernel.stub(:sleep)
58-
lambda do
57+
allow(Kernel).to receive(:sleep)
58+
expect do
5959
count_retryable(:on => RuntimeError ) { |tries, ex| raise StandardError if tries < 1 }
60-
end.should raise_error StandardError
61-
@try_count.should == 1
60+
end.to raise_error StandardError
61+
expect(@try_count).to eq(1)
6262
end
6363

6464
it 'retries three times' do
65-
Kernel.stub(:sleep)
65+
allow(Kernel).to receive(:sleep)
6666
count_retryable(:tries => 3) { |tries, ex| raise StandardError if tries < 2 }
67-
@try_count.should == 3
67+
expect(@try_count).to eq(3)
6868
end
6969

7070
it 'retries on default exception' do
71-
Kernel.should_receive(:sleep).once.with(1)
71+
expect(Kernel).to receive(:sleep).once.with(1)
7272

7373
count_retryable(:tries => 2) { |tries, ex| raise StandardError if tries < 1 }
74-
@try_count.should == 2
74+
expect(@try_count).to eq(2)
7575
end
7676

7777
it 'executes exponential backoff scheme for :sleep option' do
78-
[1, 4, 16, 64].each { |i| Kernel.should_receive(:sleep).once.ordered.with(i) }
79-
lambda do
78+
[1, 4, 16, 64].each { |i| expect(Kernel).to receive(:sleep).once.ordered.with(i) }
79+
expect do
8080
Kernel.retryable(:tries => 5, :sleep => lambda { |n| 4**n }) { raise RangeError }
81-
end.should raise_error RangeError
81+
end.to raise_error RangeError
8282
end
8383

8484
it 'does not retry any exception if :on is empty list' do
85-
lambda do
85+
expect do
8686
count_retryable(:on => []) { raise }
87-
end.should raise_error RuntimeError
88-
@try_count.should == 1
87+
end.to raise_error RuntimeError
88+
expect(@try_count).to eq(1)
8989
end
9090

9191
it 'catches an exception that matches the regex' do
92-
Kernel.should_receive(:sleep).once.with(1)
92+
expect(Kernel).to receive(:sleep).once.with(1)
9393
count_retryable(:matching => /IO timeout/) { |c,e| raise "yo, IO timeout!" if c == 0 }
94-
@try_count.should == 2
94+
expect(@try_count).to eq(2)
9595
end
9696

9797
it 'does not catch an exception that does not match the regex' do
98-
should_not_receive :sleep
99-
lambda do
98+
expect(Kernel).not_to receive(:sleep)
99+
expect do
100100
count_retryable(:matching => /TimeError/) { raise "yo, IO timeout!" }
101-
end.should raise_error RuntimeError
102-
@try_count.should == 1
101+
end.to raise_error RuntimeError
102+
expect(@try_count).to eq(1)
103103
end
104104

105105
it 'does not allow invalid options' do
106-
lambda do
106+
expect do
107107
retryable(:bad_option => 2) { raise "this is bad" }
108-
end.should raise_error ArgumentError, '[Retryable] Invalid options: bad_option'
108+
end.to raise_error ArgumentError, '[Retryable] Invalid options: bad_option'
109109
end
110110

111111
it 'accepts a callback to run after an exception is rescued' do
112-
lambda do
112+
expect do
113113
retryable(:sleep => 0, :exception_cb => Proc.new {|e| @raised = e.to_s }) {|tries, ex| raise StandardError.new("this is fun!") if tries < 1 }
114-
end.should_not raise_error
114+
end.not_to raise_error
115115

116-
@raised.should == "this is fun!"
116+
expect(@raised).to eq("this is fun!")
117117
end
118118
end

spec/spec_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'rspec'
33

44
RSpec.configure do |config|
5-
# Remove this line if you don't want RSpec's should and should_not
6-
# methods or matchers
7-
require 'rspec/expectations'
5+
config.disable_monkey_patching!
86

97
def count_retryable(*opts)
108
@try_count = 0

0 commit comments

Comments
 (0)