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

Config option for blocking mocks/stubs on specific methods or classes #613

Open
amomchilov opened this issue Jul 6, 2023 · 1 comment
Open
Assignees

Comments

@amomchilov
Copy link

amomchilov commented Jul 6, 2023

Hey there!

I have a use case where a value needs to be stubbed for tests, but Mocha isn't the right approach (because reasons1). We have our own test helper function for stubbing the values in question, but developers who don't know about it just naturally for Mocha stubs/mocks. We'd like to enforce against that.

I could write a Rubocop cop to try to catch this, but detecting these uses purely syntactically is best-effort and imperfect.

Would it make sense for Mocha to have some kind of API for blocking particular kinds of mocks? It could either be some kind of black list (where you register classes and/or methods that shouldn't be stubbed or mocked), or perhaps a hook that's called whenever a mock is registered, letting you implement your own logic to approve or reject an attempt to mock a method.

I'd gladly contribute such a change, if it's deemed worthwhile and desirable

Cheers

Footnotes

  1. The values in questions are A/B experiment treatment groups. We start with just mocking the experiments being checked in a given flow, with one Mocha expectation per experiment. This is great, but the issue comes when a new experiment check is introduced somewhere in that flow. Because Mocha stubs need to exhaustively match every set of parameters that a method is called with (else you get a ExpectationError), all the tests that hit that flow need to be updated to provide a matcher and result for that new parameter, even when the real method behaviour (of returning nil) would have been correct. We end up repeating code like .expects(:experiment_group).with(:some_new_experiment_name).returns(nil) for every such experiment, in many places.

@floehopper
Copy link
Member

Hi @amomchilov. Thanks for your suggestion. Sorry for the slow response - we've been busy with client work recently.

At first glance this seems like a bit of a niche requirement. Nobody else has asked for anything like it in 17 years! I'd be interested to understand a bit more about why you don't think you can use Mocha in the scenario you describe. Would you be able to share an illustrative test or set of tests that demonstrate the problem you're having?

If you can convince me that Mocha is definitely not suitable, then I'm not completely opposed to the idea of providing the sort of hook that you describe - I think that wouldn't add too much to our maintenance burden and would be a less niche solution.

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