Any luck with this one? I'm wondering the same thing.
unit tests and object-inheritance
Hey! I have a question concerning "unit tests" and object-inheritance. For example:
I have a class A which extends class B. Let's assume the only difference of the two classes is the add method . In class B this add method is slightly extended. Now I want to write a unit test for the add function of class B but because of the parent::add call I have a dependency to the parent class A. In this case I can't mock the add method of the parent class so the resulting test will be a integration test but if I want it to be a unit test? I don't want the the test for method add in class B fails because of the parent method in class A. In this case only the unit-test of the parent method should fail.
class B exends A
{
public function add()
{
parent::add();
//do some additional stuff
}
....
}
class A
{
public function add(){...}
....
}
Surely I could use object-aggregation and pass my parent object to the child contructor and therefore I would be able to mock the parent method add, but is this the best approach? I would rarely use object-inheritance anymore.
class B {
protected $a;
public function __contruct(A $a)
{
$this->a = $a;
}
public function add()
{
$this->a->add();
//do some additional stuff
}
....
}
class A {
public function add(){...}
....
}
I would be very grateful for your opinions. Thanks!
Karl
Please or to participate in this conversation.