Code Monkey home page Code Monkey logo

Comments (1)

ghostwriter avatar ghostwriter commented on May 25, 2024

Hey @maxbethke,

Thanks for reporting your findings.

Issue Description

When testing certain method calls on a constructor-injected mock,
Mockery is throwing a Mockery\Exception\InvalidCountException,
indicating that the method wasn't called, even though it is called in the tested code.

The specific method in question is fromYaml() from the League\OpenAPIValidation\PSR7\ValidatorBuilder class.


It looks like the mock object is properly configured to expect the fromYaml method call 2 times.

  • once inside setUp

  • once inside testThatItUsesTheApiSpecification

Each time the shouldReceive method is called on a mock object in Mockery,
it adds a new expectation for that method with the specified arguments.


The test passes, when I use PHPUnits createMock instead of mockery like this

    protected function setUp(): void
    {
        $this->validatorBuilder = $this->createMock(ValidatorBuilder::class);
        $this->validatorBuilder->expects($this->once())
                               ->method('fromYaml')
                               ->willReturn($this->validatorBuilder);
        $this->schemaValidatorService = new SchemaValidatorService($this->validatorBuilder);
    }

The equivalent Mockery code for the above code would look like this:

class SchemaValidatorServiceTest extends MockeryTestCase
{
    private $schemaValidatorService;
    private $validatorBuilder;

    protected function setUp(): void
    {
        $this->validatorBuilder = \Mockery::mock(ValidatorBuilder::class);
+        $this->validatorBuilder->shouldReceive('fromYaml')->once()->andReturnSelf();
        $this->schemaValidatorService = new SchemaValidatorService($this->validatorBuilder);
    }

    public function testThatItUsesTheApiSpecification()
    {
-        $this->validatorBuilder->shouldReceive('fromYaml')->once()->andReturnSelf();
        $this->schemaValidatorService->validateRoutedRequest();
    }

    protected function tearDown(): void
    {
        \Mockery::close();
    }
}

To summarize, the code you provided sets up 2 expectations for the mock object.

once inside setUp and another in testThatItUsesTheApiSpecification

The test fails because that method is called exactly 1 time in testThatItUsesTheApiSpecification
but expected to be called 2 times.

This is the expected behavior.

Let me know if this resolves the issue for you. (please don't forget to closing this issue.)

from mockery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.