In short:
Yes. At least that's what I'm doing right now.
Longer version:
If the expected co-authors of your class do not exist at the moment when you need them in the tests for the class you are building, you have several options :
- Layout of nonexistent classes (which phpunit can do)
- Create class skeletons and mock them
- Just create interfaces and get mocks for those (which phpunit can do too)
- Perhaps you do not need any of the above depending on the object
If you are programming against an interface , since all you have to do is create this interface and tell PHPUnit to create a / mock stub from it
- + No new class without test
- + Using interfaces, when appropriate, is considered better / better than just hinting at classes
When it makes fun of non-existing classes , you get some flaws that I don't like:
- - High maintenance costs
- -Keeping methods on these classes is slow and tedious
- -If you created a class, you must rework mocks again
so I would consult with that.
the middle way would be to simply create an empty skeleton of the class with its method and use them for bullying.
I really like this in cases where there is no interface for a hint, since it is fast and creates stable test code.
Having barebone classes with public apis is, for me, not a violation of TDD.
There are classes you don't need to make fun of.
Data Transfer Objects and Value Objects can always be created anywhere using new in your production code, so your tests can also only be real objects .
This helps keep your tests a little cleaner, since you don't have to scoff / expect a lot of getter / setter methods, etc.
edorian
source share