Skip to content

Commit

Permalink
fix- Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
morissetcl committed Jan 28, 2024
1 parent 0416901 commit c92be12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/pry-cyrano/exceptions_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ExceptionsHandler < Base
attr_reader :exception, :output, :pry

UNITIALIZED_CONSTANT = "uninitialized constant"
UNINITIALIZED_CONSTANT = "uninitialized constant"
UNDEFINED_TABLE = "UndefinedTable"

def initialize(output, exception, pry)
Expand All @@ -21,12 +21,14 @@ def call
case exception
in NameError => error
# eg: Usert.last
return pry_exception_handler unless error.message.include?(UNITIALIZED_CONSTANT)
# NameError is a Superclass for all undefined statement.
# In this context We only need to one including an `uninitialized constant` error.
return pry_exception_handler unless error.message.include?(UNINITIALIZED_CONSTANT)
Exceptions::NameError.call(output, exception, pry)
in ActiveRecord::StatementInvalid => error
# eg: User.joins(:groups).where(grous: { name: "Landlord" }).last
# ActiveRecord::StatementInvalid is a Superclass for all database execution errors.
# We only need to one including an `UndefinedTable` error.
# eg: User.joins(:groups).where(grous: { name: "Landlord" }).last
return pry_exception_handler unless error.message.include?(UNDEFINED_TABLE)
Exceptions::ActiveRecord::StatementInvalid.call(output, exception, pry)
in ActiveRecord::ConfigurationError
Expand Down
25 changes: 18 additions & 7 deletions spec/exceptions_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@
let(:application_dictionary) { {ar_models_dictionary: ["User"], associations_dictionary: ["users", "user"]} }

context "given a NameError" do
let(:exception) { NameError.new }
context "given a ActiveRecord::StatementInvalid related to uninitialized constant" do
let(:exception) { NameError.new(ExceptionsHandler::UNINITIALIZED_CONSTANT) }

it "calls Exceptions::NameError error" do
expect(Exceptions::NameError).to receive(:call).with(output, exception, pry)
subject
it "calls Exceptions::NameError error" do
expect(Exceptions::NameError).to receive(:call).with(output, exception, pry)
subject
end
end

context "given a ActiveRecord::StatementInvalid not related to uninitialized constant" do
let(:exception) { NameError.new }

it "calls Pry::ExceptionHandler" do
expect(Pry::ExceptionHandler).to receive(:handle_exception)
subject
end
end
end

context "given a ActiveRecord::StatementInvalid error" do
context "given a ActiveRecord::StatementInvalid related to undefined table" do
let(:exception) { ActiveRecord::StatementInvalid.new("PG::UndefinedTable") }
let(:exception) { ActiveRecord::StatementInvalid.new(ExceptionsHandler::UNDEFINED_TABLE) }

it "calls Exceptions::ActiveRecord::StatementInvalid" do
expect(Exceptions::ActiveRecord::StatementInvalid).to receive(:call).with(output, exception, pry)
Expand All @@ -31,8 +42,8 @@
context "given a ActiveRecord::StatementInvalid not related to undefined table" do
let(:exception) { ActiveRecord::StatementInvalid.new }

it "does not calls Exceptions::ActiveRecord::StatementInvalid" do
expect(Exceptions::ActiveRecord::StatementInvalid).not_to receive(:call)
it "calls Pry::ExceptionHandler" do
expect(Pry::ExceptionHandler).to receive(:handle_exception)
subject
end
end
Expand Down

0 comments on commit c92be12

Please sign in to comment.