rescue_me 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,5 +2,5 @@
2
2
  require "bundler/gem_tasks"
3
3
  require "rspec/core/rake_task"
4
4
 
5
- RSpec::Core::RakeTask.new(:spec)
5
+ RSpec::Core::RakeTask.new(:spec) { |t| t.rspec_opts = "--colour" }
6
6
  task :default => :spec
@@ -1,3 +1,3 @@
1
1
  module RescueMe
2
- VERSION = '0.1.2' unless defined? RescueMe::VERSION
2
+ VERSION = '0.1.3' unless defined? RescueMe::VERSION
3
3
  end
@@ -1,85 +1,60 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- # TODO AS: Replace this class with a mock
4
- class ExceptionCounter
5
-
6
- require 'net/smtp'
7
- attr_reader :method_called_count
8
-
9
- def initialize
10
- @method_called_count = 0
11
- end
12
-
13
- def exception_free
14
- @method_called_count += 1
15
- "This method does not raise exceptions"
16
- end
17
-
18
- def raise_zero_division_error
19
- @method_called_count += 1
20
- 12/0
21
- end
22
-
23
- # Will raise SMTPServerBusy the first x times this method is called,
24
- # after which this method does nothing.
25
- def raise_smtp_exception_until_call(times)
26
- @method_called_count += 1
27
- @smtp_exception_count = (@smtp_exception_count ||= 0) + 1
28
- raise Net::SMTPServerBusy if times > @smtp_exception_count
29
- end
30
-
31
- end # ExceptionCounter
3
+ class TestLogger # required because RSpec mocks have their own #warn
4
+ def warn(msg); STDERR.puts msg; end
5
+ end
32
6
 
33
7
  describe 'rescue_me' do
34
8
 
35
- before(:each) do
36
- @exception_counter = ExceptionCounter.new
37
- end
9
+ let(:logger) { TestLogger.new }
10
+ let(:test_class) { double "TestClass" }
38
11
 
39
12
  it "runs an exception-free block of code once" do
40
- rescue_and_retry {
41
- @exception_counter.exception_free
42
- }
43
- @exception_counter.method_called_count.should == 1
13
+ test_class.should_receive(:sendmail).once
14
+ rescue_and_retry { test_class.sendmail }
44
15
  end
45
16
 
46
17
  it "attempts to run a block that raises an unexpected exception only once" do
47
- expect do
48
- rescue_and_retry(5, IOError) {
49
- @exception_counter.raise_zero_division_error
50
- }
51
- end.to raise_error ZeroDivisionError
52
- @exception_counter.method_called_count.should == 1
18
+ test_class.stub(:sendmail).and_raise(NameError)
19
+ test_class.should_receive(:sendmail).once
20
+ expect {
21
+ rescue_and_retry(4, IOError) { test_class.sendmail }
22
+ }.to raise_error
53
23
  end
54
24
 
55
25
  it "re-runs the block of code for exactly max_attempt number of times" do
56
- begin
57
- rescue_and_retry(3, ZeroDivisionError) {
58
- @exception_counter.raise_zero_division_error
59
- }
60
- rescue
61
- end
62
- @exception_counter.method_called_count.should == 3
26
+ test_class.stub(:sendmail).and_raise(IOError)
27
+ test_class.should_receive(:sendmail).exactly(2).times
28
+ expect {
29
+ rescue_and_retry(2, IOError) { test_class.sendmail }
30
+ }.to raise_error
63
31
  end
64
32
 
65
33
  it "re-runs the block of code for exactly a single time when specified" do
66
- @exception_counter = ExceptionCounter.new
67
- begin
68
- rescue_and_retry(1, ZeroDivisionError) {
69
- @exception_counter.raise_zero_division_error
70
- }
71
- rescue
72
- end
73
- @exception_counter.method_called_count.should == 1
34
+ test_class.stub(:sendmail).and_raise(IOError)
35
+ test_class.should_receive(:sendmail).once
36
+ expect {
37
+ rescue_and_retry(1, IOError) { test_class.sendmail }
38
+ }.to raise_error
74
39
  end
75
40
 
76
41
  it "does not re-run the block of code after it has run successfully" do
77
- expect do
78
- rescue_and_retry(5, Net::SMTPServerBusy) {
79
- @exception_counter.raise_smtp_exception_until_call(3)
80
- }
81
- end.to_not raise_error
82
- @exception_counter.method_called_count.should == 3
42
+ test_class.stub(:sendmail) {
43
+ @x ||= 0; @x += 1; raise IOError if @x < 3
44
+ }
45
+ test_class.should_receive(:sendmail).exactly(3).times
46
+ expect {
47
+ rescue_and_retry(5, IOError) { test_class.sendmail }
48
+ }.to_not raise_error
49
+ end
50
+
51
+ it "names the enclosing method from which an exception arises" do
52
+ test_class.stub(:sendmail).and_raise(IOError)
53
+ test_class.stub(:logger).and_return(logger)
54
+ logger.should_receive(:warn).with(/rescue_me_spec/)
55
+ expect {
56
+ rescue_and_retry(1, IOError) { test_class.sendmail }
57
+ }.to raise_error
83
58
  end
84
59
 
85
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -101,7 +101,6 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .document
105
104
  - .gitignore
106
105
  - .ruby-version
107
106
  - Gemfile
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE