-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix 'undefined constant RED' error + add tests #25
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# require 'safer_rails_console/patches/sandbox/auto_rollback' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently all the patches are integration tested in a real rails setup and console. We don't That being said, I'm totally for unit testing the patches. I'd probably update the test for
|
||
|
||
describe "::SaferRailsConsole::Patches::Sandbox::AutoRollback" do | ||
describe ".handle_and_reraise_exception" do | ||
let(:mocked_ar_connection) { spy('ar_connection') } # rubocop:disable RSpec/VerifiedDoubles | ||
|
||
# mock ActiveRecord::Base.connection | ||
before do | ||
::SaferRailsConsole::Console.initialize_sandbox | ||
module ActiveRecord | ||
module Base | ||
end | ||
end | ||
allow(::ActiveRecord::Base).to receive(:connection).and_return(mocked_ar_connection) | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :ActiveRecord) | ||
end | ||
|
||
context "when raising a PG::ReadOnlySqlTransaction exception" do | ||
let(:error) { RuntimeError.new('Beware of the PG::ReadOnlySqlTransaction exception!') } | ||
|
||
it "outputs a message on stdout and forwards the exception" do | ||
expect do | ||
::SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(error) | ||
end.to raise_exception(error) | ||
.and output(/An operation could not be completed due to read-only mode./).to_stdout | ||
end | ||
end | ||
|
||
context "when raising a classic exception" do | ||
let(:error) { RuntimeError.new('normal error') } | ||
|
||
it "rollbacks, begins a new transaction and forwards the exception" do | ||
expect do | ||
::SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(error) | ||
end.to raise_exception(error) | ||
|
||
expect(mocked_ar_connection).to have_received(:rollback_db_transaction).ordered | ||
expect(mocked_ar_connection).to have_received(:begin_db_transaction).ordered | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use the entire namespace from the root to be safe -
::SaferRailsConsole::Colors::RED
. The fact thatRED
resolved correctly was serendipitous.A better way to do this would be to
include SaferRailsConsole::Colors
instead ofextend ...
, ensuring that the constants are included into the metaclass. That'd involve a little bit of shuffling withinColors
though, so no pressure and the first solution is totally adequate!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about including, but then the
color_text
won't be available.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, the shuffling I mentioned would involve putting that into a
ClassMethods
or similar module and using the popularself.included; extend ClassMethods
pattern.