Mocha::StateMachine states method stomps on states fixture
DBugger32 opened this issue · comments
Attempting to upgrade to Rails 7.1.1, and running into a namespace issue. It appears the Mocha::StateMachine collection is hijacking the states fixture.
Ruby version is 3.2.2.
After upgrading from Rails 7.0.8 to 7.1.1, I am getting this error running a test that uses the fixture named states...
NoMethodError: undefined method
display_name' for #<Mocha::StateMachine:0x0000000143434470 @name=:robonia, @current_state=nil>`
Have a simple active record class called State, that references the states table.
There are a few fixture records we use for testing.
And a test class that exists basically to test one method of the State class
require 'test_helper'
class StateTest < ActiveSupport::TestCase
fixtures :states
it 'returns #abbreviation - #name' do
assert_equal 'RB - Robonia', states(:robonia).display_name
end
end
Here is the top of the TestHelper class...
ENV['RAILS_ENV'] ||= 'test'
require_relative "../config/environment"
require 'rails/test_help'
require 'minitest/autorun'
require 'mocha/minitest'
Realize while typing this, that it might be a class loader issue of some sort.
Hmm. Mocha has implemented the Mocha::API#states
method for many years, so something must've changed in Rails. I'm not very familiar with fixtures these days, but it looks as if Rails must be magically defining a #states
method and somehow making that available from within the tests. I'm guessing that in Rails v7.0.8 this was happening after require 'mocha/minitest'
, but in Rails v7.1.1 it's happening before this point.
Looking into it a bit more, this Rails PR which was included in Rails v7.1.0.beta1 looks very suspicious. It looks like it's changed from defining the fixture accessor methods up-front to relying on a method_missing
implementation and presumably Mocha::API#states
is intercepting the relevant calls.
I can't immediate think of a nice way around this. Perhaps you could rename the fixture accessor somehow, e.g. rename the YML file to e.g. my_states.yml
and either use model_class
within the YML or ActiveRecord::TestFixtures::ClassMethods#set_fixture_class
in the test to specify the model class as State
...?
Another option might be to define another #states
method in the relevant test to "override" Mocha::API#states
and then call the fixture accessor via #method_missing
, e.g.
require 'test_helper'
class StateTest < ActiveSupport::TestCase
fixtures :states
it 'returns #abbreviation - #name' do
assert_equal 'RB - Robonia', states(:robonia).display_name
end
private
def states
method_missing(:states)
end
end
I haven't actually tried this so YMMV!
Thank you for the quick response and from saving me to having to dig into the Rails source. Will try the suggested workarounds. Will close the issue -- because this clearly not a mocha problem.
Thank you for the quick response and from saving me to having to dig into the Rails source.
No worries - I hope that was the relevant change!
Will try the suggested workarounds.
Good luck! 🤞 Let me know if they work!
Will close the issue -- because this clearly not a mocha problem.
Thanks. I think one could argue that Mocha shouldn't add a method with such a common name to the test context, but given this is the first time I've heard about this being a problem, I'm not planning to make a change at this stage.
Confess, I simply stopped using the :states fixture in the first repo. But, while bumping another app where the State class has some more meat, I had to actually implement a workaround.
- renamed
states.yml
->us_states.yml
- added
set_fixture_class(us_states: State)
toclass ActiveSupport::TestCase
- renamed all references to the :states fixture to :us_states
let(:ct) { us_states(:CT) }
And we're green again.
@DBugger32 Cool! Thanks for letting me know! 👍