rspec / rspec-mocks

RSpec's 'test double' framework, with support for stubbing and mocking
https://rspec.info
MIT License
1.16k stars 358 forks source link

Minitest integration fails to load ActiveModel methods #1202

Open fidalgo opened 6 years ago

fidalgo commented 6 years ago

Hi there! I'm using rspec-mocks (3.7.0) with minitest (5.10.3) and whenever I try to use instance_double on a Rails model, I have some missing methods, that should exist given the table mapping. I understand that Rails needs to do a first call in order to create the methods, but I would expect that to happen under the wood in our tests.

Another curious thing is if I do a drop, create and migrate the test will fail, but will pass on the second run.

If I force to load every Model in rails in the setup, all tests will pass, not matter if it's a empty database or not.

I'm not sure if this is a bug or a misconfiguration, but since my setup is uncommon I will leave here the report in case it would help.

Even with this in the configuration it will not work:

RSpec::Mocks.configuration.before_verifying_doubles do |reference|
    reference.target.define_attribute_methods
end
myronmarston commented 6 years ago

rspec-mocks, on its own, does not know anything about ActiveRecord (or Rails), so anything rails-specific isn't available directly from rspec-mocks. rspec-rails provides that integration. Here's the snippet that integrates with ActiveRecord to address this issue:

https://github.com/rspec/rspec-rails/blob/v3.7.2/lib/rspec/rails/active_record.rb#L10-L18

Your snippet is similar and looks like it should work but it's also slightly different. Can you try that snippet from rspec-rails to see if it fixes your issue?