I think you are referring to 'mocking' an Object. Injection just hides the source of the new reference. Object creation needs to be tested. Sometimes however you don't want to send a real email or queue a real test. Than a 'mocked' object can allow your test to flow. Just don't benchmark 'mocked' execution times.
Aug 10, 2016
3
Level 5
Testing and the "new" keyword
Quick question, I know when it comes to Unit tests, it's best to avoid creating new objects with the "new" keyword and use dependency injection instead. That all makes sense to me, since it allows you to mock the objects.
However, something I often see in Laravel's docs is stuff like this (actually something Taylor just tweeted):
https://pbs.twimg.com/media/Cpg68j5UsAACdlG.jpg:large
It seems like in these cases, there's no way around new'ing up an instance of an object. But would using code like that effect testability?
Please or to participate in this conversation.