I am trying to set test expectations on a mock object that is created in a data provider and passed to my test method. This is useful because I can reused my data provider across different test cases and have the tests define what to expect on the mock. However, phpunit marks this test as risky when the case passes, but correctly fails the test when it does not pass. Is this a known thing that cannot be done?
I am using phpunit v9.3
Here is a contrived example to show the problem:
<?php
use PHPUnitFrameworkMockObjectMockObject;
use PHPUnitFrameworkTestCase;
class Test extends TestCase
{
public function provideMock(): array
{
return [
[$this->createMock(DateTime::class)],
];
}
/** @dataProvider provideMock */
public function testMockPasses(MockObject $mock): void
{
$mock->expects($this->once())->method('format')->with('Y-m-d');
$mock->format('Y-m-d');
}
/** @dataProvider provideMock */
public function testMockFails(MockObject $mock): void
{
$mock->expects($this->once())->method('format')->with('Y-m-d');
$mock->format('Y-m-');
}
}
I would expect this to work fine as I am just passing the object to the method - all internal php stuff.
question from:
https://stackoverflow.com/questions/65918729/phpunit-marking-test-as-risky-when-defining-expectations-on-passed-mock 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…