Code Monkey home page Code Monkey logo

Comments (8)

DBugger32 avatar DBugger32 commented on July 3, 2024 1

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.

floehopper avatar floehopper commented on July 3, 2024

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.

floehopper avatar floehopper commented on July 3, 2024

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.

floehopper avatar floehopper commented on July 3, 2024

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.

DBugger32 avatar DBugger32 commented on July 3, 2024

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.

  1. renamed states.yml -> us_states.yml
  2. added set_fixture_class(us_states: State) to class ActiveSupport::TestCase
  3. renamed all references to the :states fixture to :us_states

let(:ct) { us_states(:CT) }

And we're green again.

from mocha.

floehopper avatar floehopper commented on July 3, 2024

@DBugger32 Cool! Thanks for letting me know! 👍

from mocha.

josefarias avatar josefarias commented on July 3, 2024

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.

floehopper avatar floehopper commented on July 3, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.