Skip to content

Commit

Permalink
Merge pull request #97 from NoRedInk/aeberlin-store-attempted-retries…
Browse files Browse the repository at this point in the history
…-in-example-metadata

Aeberlin store attempted retries in example metadata
  • Loading branch information
michaelglass authored Jun 11, 2018
2 parents 9840048 + 988f3b0 commit 4ba18d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/rspec/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ def initialize(ex, opts = {})
current_example.attempts ||= 0
end

# context.example is deprecated, but RSpec.current_example is not
# available until RSpec 3.0.
def current_example
RSpec.respond_to?(:current_example) ?
RSpec.current_example : @ex.example
@current_example ||= RSpec.current_example
end

def retry_count
Expand Down Expand Up @@ -120,6 +117,8 @@ def run
end
end

example.metadata[:retry_attempts] = self.attempts

example.clear_exception
ex.run

Expand Down
32 changes: 32 additions & 0 deletions spec/lib/rspec/retry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ class SharedError < StandardError; end
end

describe "with a list of exceptions to retry on", :retry => 2, :exceptions_to_retry => [RetryError] do
context do
let(:rspec_version) { RSpec::Core::Version::STRING }

let(:example_code) do
%{
$count ||= 0
$count += 1
raise NameError unless $count > 2
}
end

let!(:example_group) do
$count, $example_code = 0, example_code

RSpec.describe("example group", exceptions_to_retry: [NameError], retry: 3).tap do |this|
this.run # initialize for rspec 3.3+ with no examples
end
end

let(:retry_attempts) do
example_group.examples.first.metadata[:retry_attempts]
end

it 'should retry and match attempts metadata' do
example_group.example { instance_eval($example_code) }
example_group.run

expect(retry_attempts).to eq(2)
end
end

context "the example throws an exception contained in the retry list" do
it "retries the maximum number of times" do
raise RetryError unless count > 1
Expand Down

0 comments on commit 4ba18d5

Please sign in to comment.