5 ms·
> The point of private methods is to prevent what other code can call them. However, in PHP, you also cannot override private methods in a subclass. This is esp
by arkh 8y ago
> The point of private methods is to prevent what other code can call them. However, in PHP, you also cannot override private methods in a subclass. This is especially important when, for example, trying to build mocks in unit tests.
You may not be testing the right things if you have to override your methods just for your tests.
- giancarlostoro 8y agoYeah you shouldnt override any existing code when doing unit testing, only updating properties if anything... Otherwise you are taking the code beyond its scope, if you need output from a method to be something specific... Just set the variable you are storing that methods return value into to what you need that output to be.
- viraptor 8y agoThis is nice in theory, but you need to design everything to work dependency injection style (or really have a setter for everything) to achieve this. Using a random number? Store the generator. Opening a file? Use a "FooOpener". ... Sometimes it's easier just to stub what you want.
- dhimes 8y agoYeah- Sometimes I've gotten lazy and returned an object that has all the private stuff attached. Once I've verified that it works, I quit returning that object, keeping the methods private for normal use. It gives me a clear logical delineation yet allows me to test while developing.
- arkh 8y agoIf you can't put the thing you're testing (method, class, application) in the state you want to test using only public methods you are not testing something useful. If you're doing to get 100% coverage be happy: that's dead code you can remove.
- viraptor 8y agoCounterexample: Testing something that uses your language's Array.shuffle equivalent which doesn't expose the randomness provider. You can't put it in the right state without just stubbing the call and making it return the order you want.
- josteink 8y agoAlso: many languages prevent all access to private members in derived classes. This is by design: private is private. If you need derives classes-only to have access, you should typically use the “protected”-modifier. Doesn’t php have that?
- wnevets 8y agoThe problem stems from people believing that unit in unit testing means a class's method.