Testing objects with dependencies in PHPUnit

后端 未结 1 1203
我寻月下人不归
我寻月下人不归 2021-02-08 16:34

For objects which compose another object as part of their implementation, what\'s the best way to write the unit test so only the principle object gets tested? Trivial example:

相关标签:
1条回答
  • 2021-02-08 17:17

    As you seem to be aware already, Concrete Class Dependencies makes testing hard (or outright impossible). You need to decouple that dependency. A simple change, that doesn't break the existing API, is to default to the current behaviour, but provide a hook to override it. There are a number of ways that this could be implemented.

    Some languages have tools that can inject mock classes into code, but I don't know of anything like this for PHP. In most cases, you would probably be better off refactoring your code anyway.

    0 讨论(0)
提交回复
热议问题