Is your statement mocking you?

I notice that when I use mock objects, PHPUnit will correctly report the number of tests performed, but it will incorrectly report the number of statements I make. In fact, every time I mock him, this is considered another statement. A test file with 6 tests, 7 statements and each test, scoffed once, reported 6 tests, 13 statements.

Here is a test file with only one test deleted (for illustration here), plus I presented another test that does not jam to track this problem. PHPUnit reports 2 tests, 3 statements. I delete the mannequin: 1 test, 2 statements.

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; } } 

This is similar to the statement left in one of the standard methods of bullying. Is there a way to show each (passing) statement?

+6
source share
1 answer

After completing each test method, PHPUnit verifies that mock expectations are set during the test. PHPUnit_Framework_TestCase::verifyMockObjects() increases the number of claims for each layout created. You can override the method to cancel it if you really want, keeping the current number of statements, calling the parent method and subtracting the difference.

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

Of course, verifyMockObjects() will throw an assertion error exception if any expectation is unsatisfied, so you need to catch the exception and restore it after resetting the counter. I will leave it to you. :)

+8
source

Source: https://habr.com/ru/post/885543/


All Articles