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

Remove MockBuilder::setMethods() and MockBuilder::setMethodsExcept() #3769

Closed
sebastianbergmann opened this issue Jul 31, 2019 · 5 comments
Closed
Assignees
Labels
type/backward-compatibility Something will be/is intentionally broken
Milestone

Comments

@sebastianbergmann
Copy link
Owner

See #3687 (comment) for details.

@sebastianbergmann sebastianbergmann added feature-removal type/backward-compatibility Something will be/is intentionally broken labels Jul 31, 2019
@sebastianbergmann sebastianbergmann added this to the PHPUnit 10.0 milestone Jul 31, 2019
@sebastianbergmann sebastianbergmann self-assigned this Jul 31, 2019
@sebastianbergmann sebastianbergmann changed the title Remove MockBuilder::setMethods() Remove MockBuilder::setMethods() and MockBuilder::setMethodsExcept() Dec 10, 2020
@lpd-au
Copy link

lpd-au commented Aug 17, 2021

setMethods was replaced by onlyMethods (to mock existing methods) & addMethods (to add new methods), but what replaces setMethodsExcept? (Not mentioned in #3687 (comment)).

@leighman
Copy link

leighman commented Sep 9, 2021

Also interested in this ^
You can transform it to onlyMethods of anything you do mock but it's nice to explicitly mock everything except the method under test for futureproofness

@stebogit
Copy link

I second the latest comments, supporting the need of setMethodsExcept or an alternative to it.
There are cases where you do want to mock just some of the class methods. In my case I have a class fetching data from the DB and during tests I want to mock those methods reaching the DB and provide the expected/dummy data for the test.

With this comment in mind @sebastianbergmann, I agree in a test suite you consider a class/object as the unit under test, however I'd also argue that in a specific test of that suite you might (often?) test only part(s) of that object, as in maybe one or few methods at a time. Allowing to mock some of the other object's methods allows to perform those tests in isolation.

I wonder if the approach I just mentioned does make sense to you or if you had any thought or suggestions on that.

@olinox14
Copy link

I usually mock the whole SUT, except the method I'm testing.

The removal of setMethodExcept is a real regression for us, leaving us stuck with the v9...
Explicitly mocking every involved method with onlyMethods would represent a lot of work, increase the risk of error while mocking, and add an unnecessary verbosity to the tests...

Restoring this functionality would be a real plus.

@classicalguss
Copy link

What is the reasoning behind completely removing setMethodExcept and not having any alternative?
There are a lot of old frameworks that have old design approach that you might need to mock except for a few methods.

For example Yii1 have CActiveRecord classes which do a lot of things as soon as you instantiate it. You can mock it fine, but then how can you actually have a real test of the methods that you have designed yourself inside this class?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/backward-compatibility Something will be/is intentionally broken
Projects
None yet
Development

No branches or pull requests

6 participants