Skip to content

Commit

Permalink
Converted the project to using RSpec instead of the Shoulda test fram…
Browse files Browse the repository at this point in the history
…ework.
  • Loading branch information
Arild Shirazi committed Mar 29, 2013
1 parent 3539ced commit 0c7368c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 108 deletions.
30 changes: 15 additions & 15 deletions Rakefile
@@ -1,20 +1,20 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"
#require "rspec/core/rake_task"

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
# require 'rake/testtask'
# Rake::TestTask.new(:test) do |test|
# test.libs << 'lib' << 'test'
# test.pattern = 'test/**/test_*.rb'
# test.verbose = true
# end

task :default => :test
# task :default => :test

require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "rescue_me #{RescueMe::VERSION}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
# require 'rdoc/task'
# Rake::RDocTask.new do |rdoc|
# rdoc.rdoc_dir = 'rdoc'
# rdoc.title = "rescue_me #{RescueMe::VERSION}"
# rdoc.rdoc_files.include('README*')
# rdoc.rdoc_files.include('lib/**/*.rb')
# end
1 change: 1 addition & 0 deletions rescue_me.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'shoulda'
spec.add_development_dependency 'simplecov'
end
85 changes: 85 additions & 0 deletions spec/rescue_me_spec.rb
@@ -0,0 +1,85 @@
require_relative 'spec_helper'

# TODO AS: Replace this class with a mock
class ExceptionCounter

require 'net/smtp'
attr_reader :method_called_count

def initialize
@method_called_count = 0
end

def exception_free
@method_called_count += 1
"This method does not raise exceptions"
end

def raise_zero_division_error
@method_called_count += 1
12/0
end

# Will raise SMTPServerBusy the first x times this method is called,
# after which this method does nothing.
def raise_smtp_exception_until_call(times)
@method_called_count += 1
@smtp_exception_count = (@smtp_exception_count ||= 0) + 1
raise Net::SMTPServerBusy if times > @smtp_exception_count
end

end # ExceptionCounter

describe 'rescue_me' do

before(:each) do
@exception_counter = ExceptionCounter.new
end

it "runs an exception-free block of code once" do
rescue_and_retry {
@exception_counter.exception_free
}
@exception_counter.method_called_count.should == 1
end

it "attempts to run a block that raises an unexpected exception only once" do
expect do
rescue_and_retry(5, IOError) {
@exception_counter.raise_zero_division_error
}
end.to raise_error ZeroDivisionError
@exception_counter.method_called_count.should == 1
end

it "re-runs the block of code for exactly max_attempt number of times" do
begin
rescue_and_retry(3, ZeroDivisionError) {
@exception_counter.raise_zero_division_error
}
rescue
end
@exception_counter.method_called_count.should == 3
end

it "re-runs the block of code for exactly a single time when specified" do
@exception_counter = ExceptionCounter.new
begin
rescue_and_retry(1, ZeroDivisionError) {
@exception_counter.raise_zero_division_error
}
rescue
end
@exception_counter.method_called_count.should == 1
end

it "does not re-run the block of code after it has run successfully" do
expect do
rescue_and_retry(5, Net::SMTPServerBusy) {
@exception_counter.raise_smtp_exception_until_call(3)
}
end.to_not raise_error
@exception_counter.method_called_count.should == 3
end

end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,8 @@
# require 'simplecov'
# SimpleCov.start

require 'rspec'
#require 'test/unit'
#require 'shoulda'

require_relative '../lib/rescue_me'
7 changes: 0 additions & 7 deletions test/test_helper.rb

This file was deleted.

86 changes: 0 additions & 86 deletions test/test_rescue_me.rb

This file was deleted.

0 comments on commit 0c7368c

Please sign in to comment.