-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
More helpful error message when instantiating an abstract class #9474
Conversation
can you write two test cases to verify that AR::Base cant be instantiated and that an abstract class can't be instantiated? Also we would need a CHANGELOG entry explaining the change. |
@@ -1,5 +1,9 @@ | |||
## Rails 4.0.0 (unreleased) ## | |||
|
|||
* Throw NotImplementedError when trying to instantiate Base or an abstract class. |
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.
just a nitpick but it would be nice to put the constant name inside
.
added a few minor comments. |
Thanks, comments addressed in HonoreDB@f128e79. |
def test_initialize_abstract_class | ||
assert_raises(NotImplementedError) do | ||
FirstAbstractClass.new | ||
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.
you should assign the exception to check the message:
e = assert_raises(XX) {}
assert_equals "<expected>", e.message
the PR does no longer cleanly apply to master. Can you push a rebased version? |
Calling a literal ActiveRecord::Base.new raises NoMethodError, since it ends up calling Class.abstract_class? which does not exist. Similarly, instantiating an actual abstract class hits the database, when conventionally it should immediately throw NotImplementedError. ActiveRecord::Base can't be made abstract without breaking many, many things, so check for it separately.
Rebased, tests now check the error message in addition to the type. |
@HonoreDB amazing! thanks for the fast update. 💛 @carlosantoniodasilva @rafaelfranca can you take a final look? |
More helpful error message when instantiating an abstract class Conflicts: activerecord/CHANGELOG.md
Calling a literal
ActiveRecord::Base.new
raisesNoMethodError
,since it ends up calling
Class.abstract_class?
which does not exist.Similarly, instantiating an actual abstract class hits the database,
when conventionally it should immediately throw
NotImplementedError
.ActiveRecord::Base
can't be made abstract without breaking many,many things, so check for it separately.