Imagine I have the following class.
class SomeClass {
public function shortcutMethod($arg1) {
return $this->method($arg1, 'something');
}
public function method($arg1, $arg2) {
// some stuff
}
}
So the shortcutMethod
is a shortcut to the other method. Let us say I want to write a test that given and $arg1
the shortcutMethod
will correctly call method
with the correct arguments.
So far I think I figured I need to mock the class to expect a call to method
with some arguments and then call shortcutMethod
on the mock object like so (note I am using Mockery).
$mock = m::mock("SomeClass");
$mock = $mock->shouldReceive('method')->times(1)->withArgs([
'foo',
'something'
]);
$mock->shortcutMethod('foo');
This results in an exception like so shortcutMethod() does not exist on this mock object
.
Did I misunderstand the usage for mocking? I understand it makes more sense for objects that are dependency injected into the class, but what in this scenario? How would you go about it? And perhabs more importantly, is this sort of testing useless, and if so, why?
You should use mocking to mock out the dependencies of the class under test, not the class under test itself. After all, you are trying to test the real behavior of your class.
Your example is a little basic. How you would test such a class would depend on what your
method
function does. If it returns a value that is in turn returned byshortCutMethod
then I would say that your should just be asserting the output ofshortCutMethod
. Any dependencies within themethod
function should be mocked (methods belonging to other classes). I'm not that familiar with mockery, but I've given a tweaked version of your example a go.Having said that, it is possible to partially mock your class under test so that you can test the real behavior of the
shortCutMethod
function but mock out themethod
function to assert that it is called with the expected arguments. Have a look at partial mocks.http://docs.mockery.io/en/latest/reference/partial_mocks.html