Skip to content

Commit

Permalink
Refactor ENV stubbing in zendesk_sender_spec
Browse files Browse the repository at this point in the history
Upgrading to version 3 of the dotenv-rails gem causes tests in
zendesk_sender_spec to fail with the error `FrozenError: can't modify
frozen Hash: [...]`.

This is because the latest version of dotenv-rails introduces an
Autorestore feature which automatically restores the state of ENV
between rspec tests. In zendesk_sender_spec we stub the value of
ENV['ENV'] using `stub_const`, which also resets between tests and leads
to a conflict: bkeepers/dotenv#482.

This is resolved by replacing the use of `stub_const` with the
`Dotenv.modify` method (also introduced in v3), which makes dotenv-rails
fully responsible for the stubbing of environment variables and avoids
conflict.
  • Loading branch information
mpw5 committed Apr 15, 2024
1 parent bc18a7e commit 5a05066
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions spec/services/zendesk_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
}
end

before do
stub_const('ENV', ENV.to_hash.merge('ENV' => 'test_environment'))
allow(ZendeskAPI::Ticket).to receive(:create!)
before { allow(ZendeskAPI::Ticket).to receive(:create!) }

around do |example|
Dotenv.modify(ENV: 'test_environment') do
example.run
end
end

it 'calls ZendeskAPI::Ticket.create!' do
Expand Down

0 comments on commit 5a05066

Please sign in to comment.