Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spies do not register as allowed method calls when stubbing is specified #120

Closed
ciaranmcnulty opened this issue Aug 29, 2014 · 15 comments
Closed
Labels

Comments

@ciaranmcnulty
Copy link
Member

Normally Spied-on methods are included in the list of expected calls. However, this breaks if stubbing takes place beforehand

Spec:

class SpiesExampleSpec extends ObjectBehavior
{
    function it_does_not_expect_spied_on_methods(\DateTime $time)
    {
        $time->getOffset()->willReturn(-1800);

        $this->modifyTimestamp($time);

        $time->setTimestamp(1234)->shouldHaveBeenCalled();
    }
}

Object:

class SpiesExample
{
    public function modifyTimestamp(\DateTime $time)
    {
        $time->getOffset();
        $time->setTimestamp(1234);
    }
}

Output:

  10  ! it does not expect spies
      method call:
        Double\DateTime\P1->setTimestamp(1234)
      was not expected.
      Expected calls are:
        - getOffset()
@ciaranmcnulty
Copy link
Member Author

The Mock version passes as expected:

    function it_does_not_expect_spies(\DateTime $time)
    {
        $time->getOffset()->willReturn(-1800);
        $time->setTimestamp(1234)->shouldBeCalled();

        $this->modifyTimestamp($time);
    }

@stof
Copy link
Member

stof commented Aug 29, 2014

can you run composer show -i phpspec/prophecy to see which version of Prophecy you use ?

@ciaranmcnulty
Copy link
Member Author

phpspec/phpspec dev-master 5520c91
phpspec/prophecy v1.2.0

@ciaranmcnulty
Copy link
Member Author

Bumping prophecy to dev-master shows the same result

@ciaranmcnulty
Copy link
Member Author

To be clear, this version works fine:

Spec:

class SpiesExampleSpec extends ObjectBehavior
{
    function it_does_not_expect_spied_on_methods(\DateTime $time)
    {
        $this->modifyTimestamp($time);

        $time->setTimestamp(1234)->shouldHaveBeenCalled();
    }
}

Object:

class SpiesExample
{
    public function modifyTimestamp(\DateTime $time)
    {
        $time->setTimestamp(1234);
    }
}

It seems to be the mixture of stubs + spies

@stof
Copy link
Member

stof commented Aug 29, 2014

ok, this looks like a really weird bug

@ciaranmcnulty
Copy link
Member Author

From here:
https://github.com/phpspec/prophecy/blob/master/src/Prophecy/Call/CallCenter.php#L72-L82

It looks like at call time there will be an exception if the method is not mocked or stubbed, unless there are zero mocks/stubs.

@ciaranmcnulty
Copy link
Member Author

Fixing it would involve moving that check to much later in the process

@stof
Copy link
Member

stof commented Aug 29, 2014

but then, how could it pass in the second case ?

@ciaranmcnulty
Copy link
Member Author

Which case is the second case?

@stof
Copy link
Member

stof commented Aug 29, 2014

@everzet do you have an idea to fix this ?

@ciaranmcnulty
Copy link
Member Author

I would say record the calls and evaluate whether they were expected or not later on rather than immediately.

Or turn off the 'expected call' checking and just return null for unexpected ones (trolling Konstantin now) ;-)

@everzet everzet added the bug label Nov 29, 2014
@MPV
Copy link

MPV commented Oct 12, 2015

I just ran in to this issue as well (I have a stub that I expect a method to be called on, but shouldHaveBeenCalled() gives the error that @ciaranmcnulty mentioned).

@scaytrase
Copy link

Any news on this bug here in 2019?

elvetemedve pushed a commit to elvetemedve/prophecy that referenced this issue Aug 30, 2019
elvetemedve pushed a commit to elvetemedve/prophecy that referenced this issue Sep 2, 2019
elvetemedve pushed a commit to elvetemedve/prophecy that referenced this issue Sep 2, 2019
ciaranmcnulty added a commit that referenced this issue Dec 17, 2019
…-as-allowed-method-calls

Fix issue #120 by delaying unexpected method call evaluation
@jdreesen
Copy link
Contributor

Can this be closed, now that #441 is merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants