Question

I have no idea how this method works. Here is the example I am trying to use it :

namespace spec;

use PhpSpec\ObjectBehavior;

class MyObjectSpec extends ObjectBehavior
{
    /**
     * @param SomeEvent $event
     * @param SomeSubscriber $subscriber
     */
    function it_formats_the_string_as_a_header_if_underline_with_single_dashes(
        $event,   
        $subscriber
    )
    {
        $subscriber->onChange($event)->shouldBeCalled(); //RIGHT HERE

        // when
        $this->addSubscriber($subscriber);
        $this->doWhatever($event);
    }
}

class SomeEvent {}

class SomeSubscriber
{
    function onChange($event){}

    function usesOnChange(){
        $someEvent = new SomeEvent();
        $this->onChange($someEvent);
    }

}

The namespace is right and the files are in ObjectSpec.php , SomeEvent.php(in src folder) , and SomeSubscriber.php(in src folder) respectively. This gives me an error No Call has been matched but at least one was expected Why doesn't this work? Thank you.

Was it helpful?

Solution

Could you provide your implementation of MyObject? It will be helpful if you want to get a proper answer.

When you're trying to create mock of object method ->shouldBeCalled() basically means that method onChange() with parameter $event should be called on object $subscriber.

The message that you are getting means basically that method was not called and now is the step that you should implement it.

I've provided basic implementation of your class that gives successful result in PHPSpec run.

<?php

class MyObject
{
    private $subscribers = array();

    public function addSubscriber($subscriber)
    {
        $this->subscribers[] = $subscriber;
    }

    public function doWhatever($event)
    {
        foreach($this->subscribers as $subscriber) {
            $subscriber->onChange($event);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top