Skip to content

Commit

Permalink
Don't fail erroneously on unstub_const when not stubbed
Browse files Browse the repository at this point in the history
It's possible to call `unstub_const` without having called `stub_const`,
or while having already called `unstub_const`. This can happen when
there is an exception while setting up the model itself in the before
block, and `stub_const` is never called, but the after block still tries
to cleanup by calling `unstub_const`.
  • Loading branch information
amarshall committed Nov 4, 2018
1 parent 7013ec3 commit 2086d7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/with_model/constant_stubber.rb
Expand Up @@ -19,9 +19,11 @@ def stub_const(value)
end

def unstub_const
@namespace.__send__ :remove_const, basename
@namespace.const_set basename, @original_value if @original_value
@namespace = nil
if @namespace
@namespace.__send__ :remove_const, basename
@namespace.const_set basename, @original_value if @original_value
@namespace = nil
end
@original_value = nil
end

Expand Down
19 changes: 19 additions & 0 deletions spec/constant_stubber_spec.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'spec_helper'

module WithModel
describe ConstantStubber do
it 'allows calling unstub_const multiple times' do
stubber = described_class.new('Foo')
stubber.stub_const(1)
stubber.unstub_const
stubber.unstub_const
end

it 'allows calling unstub_const without stub_const' do
stubber = described_class.new('Foo')
stubber.unstub_const
end
end
end

0 comments on commit 2086d7e

Please sign in to comment.