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
Additional thoughts on constant stubbing #4
Comments
A few thoughts while it's fresh - and I'm focused on behaviour, not on implementation - but if I was explicitly creating a constant double for User and I also needed access to So, if I'm calling And of course, these doubles only get put into play if the existing constants don't exist, right? |
As things stand now, rspec-fire doesn't support this. Once you've put a test double in place of
I like that API :).
While I agree with this in principle, in practice, this was an issue I had to work around this week, and I don't think it was due to a design problem. The It's a common good practice in gems to use a top-level constant as a namespace for everything in the gem. Sometimes, this top-level constant has methods, as well; as the main entry point for usage of the gem, it makes sense to put the main APIs here. I've been working on a gem like this, and there was one of the methods on this top-level constant that I wanted to stub, and I wanted to use an So, it seems to me that there's a valid case for doing it both ways. As such, maybe it should default to one way but provide an option to do the other way? Something like
Nope. When you use
|
I don't have strong feelings either way, happy to trust your judgement. (Sorry that's a total cop-out, but I figure better than not saying anything...) |
Closing since my stuff has been merged. |
First off, thanks for merging my other PR. It's nice to see my contribution make it in :).
The conversation there and the README hints at the fact that the constant stubbing will likely become the default and potentially only behavior for
fire_class_double
. While I like the idea in principle, in practice I think there's an issue we need to resolve first. It works great for "terminal" constants (that is, constants that don't have constants nested within them) but there's a problem when stubbing a non-terminal constant. Specifically, once you've stubbed the constant, you can no longer access the nested constants until the example is finished and the original constant chain has been restored. As an example, consider this code:If you use
fire_replaced_class_double("User")
, then referencingUser::MAX_PROJECTS
will result in aTypeError
("is not a class/module"). Likewise, all access is cut off toUser::EmailParser
.I've thought a bit about how we might deal with this, and here's what I have in mind:
fire_class_double
to return a double that actually is a module or class. Currently it's a subclass ofFireDouble
, which is a subclass ofRSpec::Mocks::Mock
, so this would involve refactoring the code to use composition rather than inheritance, or putting the shared logic into a mixin.I think this would solve these problems quite nicely. The one big question in my mind is, what does subclassing
RSpec::Mocks::Mock
give us that we'd be giving up by using an empty module in its place? Granted, I can go look at the rspec-mocks source, but I figured I'd ask your thoughts on this first :).Anyhow, I've been enjoying contributing to rspec-fire, so I'd be happy to implement this if we can agree on how it should work.
@garybernhardt and @freelancing-god -- given your participation on the other thread, it'd be great to get your feedback here, too.
The text was updated successfully, but these errors were encountered: