Comments (9)
I read through the linked issues, and I'm not sure the workaround will work in my situation - I'm trying to mock an object from a 3rd party gem and am unable to change their implementation to a different delegation strategy
from mocha.
@philipchan-shopify Hi. Sorry - I don't have a lot of time to look at this right now, but I suspect it's somewhat related to #30 and/or #589. I don't know if any of the comments on those issues give you any ideas how to work around the problem...?
from mocha.
I'm adding this because it took me a while to find it!
from mocha.
So just to double-check, it's the 3rd party gem that's using DelegateClass
...?
from mocha.
I had a few minutes to look at this and I've got a bit further.
I think that at least part of the problem is that the anonymous class constructed by calling DelegateClass(A)
inherits from BasicObject
and not Object
which means it's missing Mocha::ObjectMethods
in its ancestor chain which is what defines #stubs
.
However, things are further complicated because it looks like calling DelegateClass(A)
re-defines #stubs
on the anonymous class that it returns (because A#stubs
does exist). See the following code:
https://github.com/ruby/ruby/blob/909afcb4fca393ce75cc63edc7656fd95a64f0f9/lib/delegate.rb#L417-L419
I think the latter explains why calling b.stubs(...)
doesn't raise a NoMethodError
. However, even though it exists presumably something about the way the method is "copied" means it doesn't work correctly!
Some illustrative output:
class A; end
AA = DelegateClass(A)
class B < AA; end
p Object.ancestors # => [Object, Mocha::ObjectMethods, Mocha::Inspect::ObjectMethods, Mocha::ParameterMatchers::InstanceMethods, Minitest::Expectations, Kernel, BasicObject]
p A.ancestors # => [A, Object, Mocha::ObjectMethods, Mocha::Inspect::ObjectMethods, Mocha::ParameterMatchers::InstanceMethods, Minitest::Expectations, Kernel, BasicObject]
p AA.ancestors # => [AA, Delegator, #<Module:0x00000001041df1f0>, BasicObject]
p B.ancestors # => [B, AA, Delegator, #<Module:0x00000001041df1f0>, BasicObject]
b = B.new(A.new)
p b.method(:stubs).owner # => AA
from mocha.
I have a possible solution, although it reaches into the innards of Delegate
and I've only tested it on Ruby v3.2.0. Add the following before the call to DelegateClass
:
require "mocha/object_methods"
Delegator.instance_variable_set(:@delegator_api, Delegator.public_api + Mocha::ObjectMethods.instance_methods)
BasicObject.send(:include, Mocha::ObjectMethods)
This does two things:
- Adds critical methods of Mocha's API to
Delegate
's list of API methods. This means these methods won't be copied onto the class returned from callingDelegateClass
. - Adds the critical methods of Mocha's API to
BasicObject
. The class returned fromDelegateClass
inherits fromDelegate
which in turn inherits fromBasicObect
. So similarly to the example in #589, we have to include theMocha::ObjectMethods
module intoBasicObject
.
It's worth noting that this permanently includes Mocha::ObjectMethods
into BasicObject
which might give you unexpected behaviour elsewhere in your test suite. Also relying on the existence of the undocumented @delegator_api
instance variable and its associated Delegator.public_api
method is potentially brittle and may not work on other Ruby versions.
Can you give that a try and see whether it works in your use case?
from mocha.
@philipchan-shopify Having spent a bunch of time investigating this, I'd really appreciate some feedback on my suggested solution - thanks in anticipation!
from mocha.
Since I've heard nothing to the contrary, I'm going to close this.
from mocha.
See also #622.
from mocha.
Related Issues (20)
- Upgraded from 1.0 to 2.0. HOT 9
- Config option for blocking mocks/stubs on specific methods or classes HOT 1
- Mocha is incompatible with minitest v5.19 and later HOT 16
- Replace references to `MiniTest` with `Minitest` and generally tidy this stuff up HOT 1
- Consider adding an option to disable backtrack filtering
- Unstub in the `then` clause
- Stub by id
- Mocha::StateMachine states method stomps on states fixture HOT 8
- 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
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.