Weird error using Mockery while testing

Posted by Andy Huggins on April 26, 2016

The error I received when running the full suite of tests was this:

Mockery\Exception\InvalidCountException: Method get("http://someurl.com", array('query'=>'array(...)',)) from Mockery_0_GuzzleHttp_Client should be called exactly 1 times but called 0 times.

The 'http://someurl.com' is just a place holder, the right url was removed, because it doesn't matter.

Now I will point out that the error said it was happening in a SpecificControllerTest@testIndex, but when I look at that method, the test is straight forward and has nothing to do with the call to 'http://someurl.com'.

This made me think a bit. What is going on here? How is that even getting called?

After thinking a bit, some Googling, I asked my coworker who pointed me to a mocked Guzzle call. Ok, but that is in a different test file and a different method, why is it affecting this other test?

Well, with Mockery, you have to clean up after it. I guess Mockery does not know when the test has completed, so if you set up an expectation, it just sits there until the expectation is met. So this is why you see things like this on your test classes:

public function tearDown()
{
    m::close();
    parent::tearDown();
}

The reason I mention it, is because it's kind of a weird issue to track down. I had not written the other test, but it looked as if the error was coming in a test I had written. Without cleaning up Mockery, the expectation just hung around. So you may have to do some detective work in order to track down the real issue.