Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted the project to using RSpec instead of the Shoulda test fram…
…ework.
- Loading branch information
Arild Shirazi
committed
Mar 29, 2013
1 parent
3539ced
commit 0c7368c
Showing
6 changed files
with
109 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# require 'simplecov' | ||
# SimpleCov.start | ||
|
||
require 'rspec' | ||
#require 'test/unit' | ||
#require 'shoulda' | ||
|
||
require_relative '../lib/rescue_me' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.