Comments (1)
Hey @maxbethke,
Thanks for reporting your findings.
Issue Description
When testing certain method calls on a constructor-injected mock,
Mockery is throwing aMockery\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 theLeague\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 thisprotected 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)
- mockery/mockery: 1.4.x-dev not found HOT 3
- Factory methods for `IsEqual` and `IsSame` matchers HOT 1
- support for PHPUnit constraints HOT 2
- Add "set property" method to mock object? HOT 1
- Disabled travis-ci and sensiolabs webhooks
- [New feature] Running code after mock method was called HOT 2
- [Feature request] Add array-like objects support to argument matchers
- PHP return type mismatch error when mocking method with static return type HOT 2
- Deprecations about drop support Metadata doc-comment in PHPUnit 12 HOT 2
- Latest release totally broken HOT 14
- PHPStan error with mock method in 1.6.8 HOT 1
- Invalid `Mockery::mock()`/`spy()` phpdoc (= phpstan errors) HOT 6
- Issues with partial construction trait methods on 1.6.10 - change in behaviour HOT 1
- Cannot use 'false' as class name as it is reserved HOT 2
- Typings for setConstantsMap does not allow array HOT 1
- Mocking final classes reports unresolvable type by PHPStan HOT 1
- Error when mocking anonymous classes HOT 5
- Received `*::__construct()` but no expectations were specified HOT 1
- 1.6.12 typehint issue with spy methods HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockery.