dimanche 8 février 2015

Testing a method that calls other method

I'm wondering, how do I test a method that calls other method? Is it better to expect a certain result from the other method or expect that the method is going to call the other method?


I'm in this situation: I have an abstract class, and that class has two public method, and one private, the two public methods calls the private one. I can mock the abstract class and pass it to the invoke method of the reflection, but if I try to expect a method of the mock to be called, it doesn't work.



abstract class Foo
{
public function A()
{
$this->C('foo');
}
public function B()
{
$this->C('bar');
}

private function C($arg)
{
//...
}
}

// test
$foo = $this->getMockForAbstractClass(Foo::class);
$method = new ReflectionMethod(Foo::class, 'C');
$method->setAccessible(true);
$foo->expects($this->once())->method('C')->with('foo');
$method->invoke($foo, 'foo');


It doesn't work.


Which option is the best (or the right)? Is it possible to do what I'm trying to do? Expect a private method from an abstract class to be called from a public method?


Aucun commentaire:

Enregistrer un commentaire