phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 242 forks source link

`shouldNotBeCalled` does not raise error when the method is called #617

Open Deuchnord opened 8 months ago

Deuchnord commented 8 months ago

Given the following class:

class SomeClass
{
    public function foo(): Foo
    {
        // some logic here
        return $foo;
    }
}

and the following test:

$mock = $this->prophesize(SomeClass::class);
$mock->foo()->shouldNotBeCalled();

$obj = new Bar($mock->reveal());
$obj->doSomething();

If the foo() method is called during the test, we get the error "Return value must be of type Foo, null returned" instead of the "No calls expected that match: SomeClass::foo()" from NoCallsPrediction.

This makes debugging tests very hard, because the error suggests that we accidentally returned null in a method that, in fact, should not have been called.

I have made a reproducer for this issue to make it more clear: https://github.com/Deuchnord/prophecy-reproducer