Comments (8)
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.
from mocha.
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
...?
from mocha.
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!
from mocha.
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.
from mocha.
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.
from mocha.
@DBugger32 Cool! Thanks for letting me know! 👍
from mocha.
Just ran into this today. I'm only planning on accessing my states
fixtures from my system tests so was able to patch like this:
❯ git diff
diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb
index 5960411..6645a24 100644
--- a/test/application_system_test_case.rb
+++ b/test/application_system_test_case.rb
@@ -1,3 +1,8 @@
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome
+
+ def states(...)
+ # https://github.com/freerange/mocha/issues/620
+ access_fixture("states", ...)
+ end
end
And then have my system tests inherit from ApplicationSystemTestCase
This is good enough for me because I don't plan to use Mocha's state machine API. But wanted to add a data point to this conversation.
FWIW, my states
fixtures represent a collection of US states.
from mocha.
@josefarias Thanks for posting this. I'm not sure there's much more Mocha can do about this. Since Rails is now relying on method_missing
, I think perhaps Rails should include a check that the method hasn't already been defined. I'll give this some more thought.
from mocha.
Related Issues (20)
- Stubs on instances of DelegateClass do not behave as expected HOT 9
- Add docs about issues & workarounds for using Mocha with BasicObject & the various Delegator classes
- YARD docs for Mock#method_missing is broken HOT 1
- Update Ruby Hash syntax in docs
- Drop support for Ruby v2.0
- Drop support for Ruby v2.1
- Document some of the history of Mocha
- Improve failure message
- sometimes method fails to be mocked HOT 8
- Sign releases
- Stubbed methods not cleaned up post spec completion HOT 1
- Consider using GitHub-flavoured markdown notes, warnings, etc in README
- Mocha and Rails 7.0+ and controller tests HOT 3
- Run CI build with `--enable-frozen-string-literal` Ruby command line option
- Fix Circle CI builds for Ruby v2.0 or discontinue support
- undefined method `sequences' HOT 5
- Better error message if `Mockery.teardown` is called when `Mockery.setup` has not been called successfully
- Parameter matching fails on keyword arguments HOT 5
- Consider using Ruby v1.9 hash syntax in `Hash#mocha_inspect`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mocha.