Skip to content
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

Mocha and Rails 7.0+ and controller tests #639

Open
zenspider opened this issue Feb 22, 2024 · 3 comments
Open

Mocha and Rails 7.0+ and controller tests #639

zenspider opened this issue Feb 22, 2024 · 3 comments

Comments

@zenspider
Copy link
Contributor

I'm in the midst of an upgrade and getting bit by mocha a fair amount and don't see any related issues. Namely this:

https://github.com/rails/rails/blob/main/actionpack/lib/action_controller/test_case.rb#L504

Is clearing out the @mocha ivar stored on the test controller on EVERY request such that any test with 2+ requests will have mocha fail any expectations on the second request because the mockery (?) gets wiped out.

A possible workaround is to pop this into the topmost setup/before:

    # forces R7 controller ivar cleaning to keep expectations across requests
    @controller.instance_variable_set(:@mocha, nil)

but I'm having to do that in a lot of controller tests and I figure I'm doing something terribly wrong.

How am I the only one reporting this? Are we doing something weird? Are we the only ones left still using controller tests? Integration tests are a HUGE leap for us and I imagine a lot of legacy rails apps.

@zenspider
Copy link
Contributor Author

Example of this going bad:

#>>> puts exception
unexpected invocation: #<Api::V2::TicketFieldsController:0x3c528>.statsd_client()
unsatisfied expectations:
- expected exactly once, invoked never: #<Api::V2::TicketFieldsController:0x3c528>.statsd_client(any_parameters)
...

the stubbed method on the controller in still the mocha bypass, but with the mockery nilled out the call is "unexpected".

@floehopper
Copy link
Member

floehopper commented Feb 24, 2024

I'm dealing with a family bereavement at the moment, so I don't have much time to investigate this.

I've been using controller tests in a Rails 7 project recently and haven't come across this issue, although to be fair I can't be sure we did any stubbing of controller methods.

Making multiple requests in a single controller test method seems somewhat unusual to me - perhaps that's what makes your use case different - or have I misunderstood?

If you can share a minimal example of code that reproduces the problem, I'll try to find some time to investigate.

@floehopper
Copy link
Member

@zenspider Is this still causing a problem for you?

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

No branches or pull requests

2 participants