Skip to content

Fix verified partial doubles with abstract classes. #1396

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

Merged
merged 2 commits into from
Jun 16, 2015

Conversation

JonRowe
Copy link
Member

@JonRowe JonRowe commented Jun 15, 2015

As reported in rspec/rspec-mocks#975 abstract classes can't be verified currently due to the callback for defining attribute methods. This corrects that.

Fixes rspec/rspec-mocks#975.

JonRowe added a commit that referenced this pull request Jun 15, 2015
@@ -11,7 +11,7 @@ def self.initialize_activerecord_configuration(config)
::RSpec::Mocks.configuration.when_declaring_verifying_double do |possible_model|
target = possible_model.target

if target.respond_to?(:define_attribute_methods) && ActiveRecord::Base != target
if target.respond_to?(:define_attribute_methods) && ActiveRecord::Base != target && !target.abstract_class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why abstract_class instead of abstract_class?

http://api.rubyonrails.org/classes/ActiveRecord/Inheritance/ClassMethods.html#method-i-abstract_class-3F

Also, I don't think that you can assume target will respond to abstract_class just because it responds to define_attribute_methods. Consider that ActiveModel providesdefine_attribute_methods as well:

http://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-define_attribute_methods

So this needs a respond_to?(:abstract_class) check.

On a side note, I was hoping that ActiveRecord::Base.abstract_class? would return true (it is abstract after all...) so that we could do away with the explicit ActiveRecord::Base != target check but sadly, it doesn't :(.

@myronmarston
Copy link
Member

LGTM besides the concerns raised in my comment above.

@@ -1,4 +1,4 @@
tmp
pmp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to change this?

@cupakromer
Copy link
Member

LGTM. I agree with Myron's comment.

@JonRowe JonRowe force-pushed the fix_abstract_class_allow branch from bd7bf67 to eeb450b Compare June 16, 2015 05:35
@JonRowe
Copy link
Member Author

JonRowe commented Jun 16, 2015

Changed and added a spec for active model based classes

@JonRowe JonRowe force-pushed the fix_abstract_class_allow branch from eeb450b to 2ebc177 Compare June 16, 2015 05:37
@myronmarston
Copy link
Member

LGTM

@myronmarston
Copy link
Member

@JonRowe, before merging this, take a look at the conversation here:

rspec/rspec-core#1994 (comment)

I think it could be worthwhile to make further changes to the check in the callback here.

JonRowe added a commit that referenced this pull request Jun 16, 2015
Fix verified partial doubles with abstract classes.
@JonRowe JonRowe merged commit f270e29 into master Jun 16, 2015
@JonRowe JonRowe deleted the fix_abstract_class_allow branch June 16, 2015 08:23
@JonRowe
Copy link
Member Author

JonRowe commented Jun 16, 2015

Possibly but I think there might be further discussion needed given a more complex interaction, this will allow the original issue to be solved in master (possivly even released if it takes us a while) while we do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants