DEV Community

Discussion on: Setting private properties in tests

Collapse
 
matthewbdaly profile image
Matthew Daly

Mocks aren't useful in this case.

This particular use case was a test for a read-only Doctrine entity. The ID is something that would never be set manually, only by getting it back from a Doctrine repository. As such, there was no reason to mock anything, and no reason to create a setter method just for test purposes.

To be fair, this is very much an edge case, though.

Collapse
 
vlasales profile image
Vlastimil Pospichal

How do you get the property name? It is private and may change at any time in subsequent versions.

Thread Thread
 
matthewbdaly profile image
Matthew Daly

Well, this is my own project I'm testing. If the property name does change in future either I'll know and will remember to update it in the test, or I'll forget and the tests will break in Travis CI, reminding me to fix them. Given that this is a Doctrine entity and so it expects the property in question to be called $id, that's fairly unlikely.