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

InvalidCountException even though the mock method is called #1352

Closed
maxbethke opened this issue Jan 2, 2024 · 1 comment
Closed

InvalidCountException even though the mock method is called #1352

maxbethke opened this issue Jan 2, 2024 · 1 comment
Assignees
Labels
Question looking for information

Comments

@maxbethke
Copy link

Mockery Version

1.6.7

PHP Version

PHP 8.3

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.

Steps to Reproduce

The following code can be used to reproduce the issue:

<?php

require 'vendor/autoload.php';

use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class SchemaValidatorService
{
    public function __construct(private ValidatorBuilder $validatorBuilder)
    {
    }

    public function validateRoutedRequest()
    {
        return $this->validatorBuilder->fromYaml('testYaml')->getRoutedRequestValidator();
    }
}

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

    protected function setUp(): void
    {
        $this->validatorBuilder = \Mockery::mock(ValidatorBuilder::class);
        $this->validatorBuilder->shouldReceive('fromYaml')->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();
    }
}

Expected Behavior

The test should pass

Actual Behavior

Mockery\Exception\InvalidCountException : Method fromYaml(<Any Arguments>) from Mockery_1_League_OpenAPIValidation_PSR7_ValidatorBuilder should be called exactly 1 times but called 0 times. is thown and the test fails

Exception or Error

Mockery\Exception\InvalidCountException

Additional Information

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);
    }
@maxbethke maxbethke added the triage needs to be triaged label Jan 2, 2024
@ghostwriter
Copy link
Member

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.)

@ghostwriter ghostwriter self-assigned this Jan 2, 2024
@ghostwriter ghostwriter added Awaiting Reply issue or pull request requires a reply from the author. and removed triage needs to be triaged labels Jan 2, 2024
@ghostwriter ghostwriter added Bug An error or unexpected behavior. Question looking for information and removed Awaiting Reply issue or pull request requires a reply from the author. labels Feb 5, 2024
@ghostwriter ghostwriter removed the Bug An error or unexpected behavior. label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question looking for information
Projects
None yet
Development

No branches or pull requests

2 participants