0

I am trying to test a class method that gets its data from another method.
So I did this :

function it_should_return_json_file_as_array()
{
    $this->exist()->willReturn(true);
    $this->read()->willReturn("{\"key\":\"value\"}");
    $this->getContent()->shouldHaveKeyWithValue('key', 'value');
}

But when I launched phpspec run, I got this:

[InvalidArgumentException]
String "" is not a valid classname. 
Please see reference document: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md  

What is going on?

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116

1 Answers1

0

It is not possible to stub methods of a class you are specifying, and it is not possible on purpose.

PhpSpec is trying to tell you there's a problem with the way you decided to design.

See My top ten favourite PhpSpec limitations - Limitation #2:

This limitation can become very visible in the case of using inheritance to extend behaviour. We inherit a class and want to add a new method that internally delegates some behaviour to a parent class method. We cannot mock or stub that method. This has lead me again and again to favour composition over inheritance, which is a golden principle in OO design. In the few cases in which inheritance is justified, we can isolate the reusable behaviour into a separate object and use composition in the parent object, allowing us to replace the collaborator with a double.

Also read Partial Mocking.

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125