Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

php - PHPUnit marking test as risky when defining expectations on passed mock

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try running PHPUnit with --verbose key - it may tell more about the reason of marking the test as risky.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...