I have a class structure like this:
class A {
public someFunction() {
$objectB = new B();
$result = $objectB->getResult();
return $result;
}
}
I am writing the unit test for someFunction() that belongs to class A. However, it depends on class B. I can mock someFunction() but how can I resolve the dependency on class B? I want to mock class B automatically.
Use dependency injection: either provide a method to set the B object, or pass the b object optionally to
someFunction().Original code
Optional parameter
Setter method