Phpunit 模拟是否会影响断言计数?

Phpunit 模拟是否会影响断言计数?,phpunit,Phpunit,我注意到,当我使用mock对象时,PHPUnit将正确地报告执行的测试数量,但不正确地报告我正在进行的断言数量。事实上,每次我嘲笑它都算作另一个断言。一个包含6个测试、7个断言语句的测试文件,每个测试模拟报告了6个测试、13个断言 require_once '..\src\AntProxy.php'; class AntProxyTest extends PHPUnit_Framework_TestCase { const sample_client_id = '495d179b948

我注意到,当我使用mock对象时,PHPUnit将正确地报告执行的测试数量,但不正确地报告我正在进行的断言数量。事实上,每次我嘲笑它都算作另一个断言。一个包含6个测试、7个断言语句的测试文件,每个测试模拟报告了6个测试、13个断言

require_once '..\src\AntProxy.php';

class AntProxyTest extends PHPUnit_Framework_TestCase {
    const sample_client_id = '495d179b94879240799f69e9fc868234';    
    const timezone = 'Australia/Sydney';
    const stubbed_ant = "stubbed ant";
    const date_format = "Y";

    public function testBlankCategoryIfNoCacheExists() {
        $cat = '';
        $cache_filename = $cat.'.xml';
        if (file_exists($cache_filename))
            unlink($cache_filename);

        $stub = $this->stub_Freshant($cat);

        $expected_output = self::stubbed_ant;
        $actual_output = $stub->getant();
        $this->assertEquals($expected_output, $actual_output);
    }

    public function testDummyWithoutStubbing() {
        $nostub = new AntProxy(self::sample_client_id, '', self::timezone, self::date_format);
        $this->assertTrue(true);
    }    

    private function stub_FreshAnt($cat) {
        $stub = $this->getMockBuilder('AntProxy')
                     ->setMethods(array('getFreshAnt'))
                     ->setConstructorArgs(array(self::sample_client_id, $cat, self::timezone, self::date_format))
                     ->getMock();

        $stub->expects($this->any())
             ->method('getFreshAnt')
             ->will($this->returnValue(self::stubbed_ant));

        return $stub;
    }
}
这是除去一个测试以外的所有测试的测试文件(用于这里的说明),另外,我引入了另一个测试,它没有存根来跟踪这个问题。PHPUnit报告了2个测试,3个断言。我移除了虚拟:1个测试,2个断言

require_once '..\src\AntProxy.php';

class AntProxyTest extends PHPUnit_Framework_TestCase {
    const sample_client_id = '495d179b94879240799f69e9fc868234';    
    const timezone = 'Australia/Sydney';
    const stubbed_ant = "stubbed ant";
    const date_format = "Y";

    public function testBlankCategoryIfNoCacheExists() {
        $cat = '';
        $cache_filename = $cat.'.xml';
        if (file_exists($cache_filename))
            unlink($cache_filename);

        $stub = $this->stub_Freshant($cat);

        $expected_output = self::stubbed_ant;
        $actual_output = $stub->getant();
        $this->assertEquals($expected_output, $actual_output);
    }

    public function testDummyWithoutStubbing() {
        $nostub = new AntProxy(self::sample_client_id, '', self::timezone, self::date_format);
        $this->assertTrue(true);
    }    

    private function stub_FreshAnt($cat) {
        $stub = $this->getMockBuilder('AntProxy')
                     ->setMethods(array('getFreshAnt'))
                     ->setConstructorArgs(array(self::sample_client_id, $cat, self::timezone, self::date_format))
                     ->getMock();

        $stub->expects($this->any())
             ->method('getFreshAnt')
             ->will($this->returnValue(self::stubbed_ant));

        return $stub;
    }
}

这就像一个断言被遗留在框架的一个模拟方法中。是否有办法显示每个(通过的)断言?

每个测试方法完成后,PHPUnit在测试期间验证模拟期望设置
PHPUnit\u Framework\u TestCase::verifyMockObjects()
增加创建的每个模拟对象的断言数量。如果确实需要,可以通过存储断言的当前数量、调用父方法并减去差值来重写该方法以撤消此操作

protected function verifyMockObjects()
{
    $count = $this->getNumAssertions();
    parent::verifyMockObjects();
    $this->addToAssertionCount($count - $this->getNumAssertions());
}

当然,如果任何期望未得到满足,
verifyMockObjects()
将抛出断言失败异常,因此您需要捕获异常并在重置计数后重新显示它。我把这件事留给你

所以我看到的行为是故意的。verifyMockObjects()递增断言计数器肯定有充分的理由,这样我就不会重写它。我将接受它,我也不希望在测试代码中使用变通方法。也许除了我以外的每个人都明白为什么会这样,但我很惊讶我在任何地方都找不到它。当你在测试中使用mock时,这是有意义的:“调用X会导致它在mock上调用Y。如果没有,测试就被破坏了。”但当你使用mock作为存根来简化测试时,这只是噪音:“调用X需要调用Y,但它只需要
false
就可以返回该值。”最后,我不使用断言的数量来表示任何内容,所以这并不重要。