New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHPUnit - Spy assertions aren't updating expectation count #785
Comments
There's nothing better you can do that I can think of, this is a bug that needs fixing. |
If you use the But I think you have a typo in your code, you shouldn't call Basically in your code example it's like calling the method on the EventListener instead of setting up Mockery: This is fine: class GreenTests extends \PHPUnit\Framework\TestCase
{
use MockeryPHPUnitIntegration;
public function testNotReceived()
{
$eventListener = \Mockery::spy(EventTrackerInterface::class);
$eventListener->shouldNotHaveReceived('handle');
}
} This fails with 'no test' error: class RedTests extends \PHPUnit\Framework\TestCase
{
use MockeryPHPUnitIntegration;
public function testNotReceived()
{
$eventListener = \Mockery::spy(EventTrackerInterface::class);
$eventListener->shouldHaveNotReceived('handle');
}
} So I think this is a non-issue /copy @davedevelopment @dallincoons |
Both of these result in "risky" tests for me:
I am using the integration trait. I only see this issue with Spies, not Mocks. |
Same problem here. Looking into the code it seems like the directors for |
In sebastianbergmann/phpunit#2484 (comment), @sebastianbergmann mentioned that there is a |
Well, yes, but this is not better than manually putting |
@Merenptah shouldHaveReceived and shouldNotHaveReceived both do increase the expectations count: mockery/library/Mockery/Mock.php Line 738 in 561fe6d
and mockery/library/Mockery/Mock.php Line 755 in 561fe6d
which then gets added to the total count: mockery/library/Mockery/Mock.php Lines 511 to 513 in 561fe6d
Our tests show that assertion count does get incremented: mockery/tests/Mockery/SpyTest.php Lines 89 to 104 in 561fe6d
Both of these examples pass with 3 assertions: <?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
require_once 'vendor/autoload.php';
class Issue785TearDownTest extends TestCase
{
public function tearDown()
{
if ($container = \Mockery::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}
parent::tearDown();
}
public function testSpy()
{
$foo = \Mockery::spy('Foo');
$foo->shouldNotReceive('spam');
$foo->bar();
$foo->shouldHaveReceived('bar');
$foo->shouldNotHaveReceived('baz');
}
} <?php declare(strict_types=1);
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
require_once 'vendor/autoload.php';
class Issue785Test extends TestCase
{
use MockeryPHPUnitIntegration;
public function testSpy()
{
$foo = \Mockery::spy('Foo');
$foo->shouldNotReceive('spam');
$foo->bar();
$foo->shouldHaveReceived('bar');
$foo->shouldNotHaveReceived('baz');
}
} robert@odin ~/projects/mockery[fix/issue785*]$ ./vendor/bin/phpunit adhoc-tests/issue785Test.php
PHPUnit 6.4.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.1.13 with Xdebug 2.5.5
Configuration: /home/robert/projects/mockery/phpunit.xml
. 1 / 1 (100%)
Time: 335 ms, Memory: 10.00MB
OK (1 test, 3 assertions)
Generating code coverage report in HTML format ... done
robert@odin ~/projects/mockery[fix/issue785*]$ ./vendor/bin/phpunit adhoc-tests/issue785TearDownTest.php
PHPUnit 6.4.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.1.13 with Xdebug 2.5.5
Configuration: /home/robert/projects/mockery/phpunit.xml
. 1 / 1 (100%)
Time: 334 ms, Memory: 10.00MB
OK (1 test, 3 assertions)
Generating code coverage report in HTML format ... done I honestly don't see a bug here. I'm going to close this issue as not reproducible. If you still think this is a bug, please open a new issue providing mockery version used, php version, phpunit version, the test case where this bug occurs and your phpunit.xml file as well. Thank you! |
True for Mockery 1.x. But a lot of people cannot use this, because they cannot use php 7. |
Mockery 1 supports php 5.6 |
Still on 5.5, nothing I can do against this ;) But it's ok, I can cope with this behavior. |
Same here. PHP 7.1.14-1+ubuntu14.04.1 It's a Cloud9 IDE Docker container. |
@rafaelbeckel what version of Mockery? |
"phpunit/phpunit": "^7.0" |
@rafaelbeckel that's interesting... Do you have any code that I can use to reproduce the issue? Could you maybe set up a test repository with the minimal code that it takes to show the bug? That would be really helpful, thank you! |
public function test_Router_calls_the_right_controller_and_action()
{
$request = new Request( 'GET', '/foo' );
$router = new Router([[ 'GET', '/foo', 'Bar@baz' ]]);
// Router will ask the factory to build the correct Controller
$factory = m::mock(ControllerFactory::class);
$factory->shouldReceive('build')
->with('Bar')->once()
->andReturn(m::spy(Controller::class));
// Let's call our router
$router->setControllerFactory($factory);
$controller = $router->getControllerFor($request);
// Router should have called the right action in the controller
$controller->shouldHaveReceived('setAction')->with('baz')->once();
$controller->shouldHaveReceived('setArguments')->with([])->once();
} Results in:
|
@rafaelbeckel in the example you provided, when are you calling |
In the tearDown() method. |
I think I have a similar issue upgrading to phpunit 6 and mockery 1.1.0, with tests written for phpunit 4 and mockery ~0.9 in setUp() I have this stub:
My test looks like this, and used to pass:
But now the tests fail with I'm using the |
@rcwsr Please read the documentation section on PHPUnit listeners, that should give you a good hint what to do. |
Thanks but that didn't really help. I am already using the TestListener, and calling Mockery::close() in tearDown directly doesn't affect the outcome. |
This means that you don't call the |
We're probably not inspecting the defaults when adding to the assertion count? |
@robertbasic please read my replies: I am using the |
@rcwsr @robertbasic see #910 |
@rcwsr sorry, somehow I've read that you're using the trait as that you're using the listener. Brains are weird. |
When using mocky assertions with PHPUnit 6.3:
I can update the assertion count by doing something like
Which is kind of annoying but ok. As long as it gets rid of the warning about no assertions.
This doesn't work however:
Since apparently spy assertions don't update the exceptations and thus the expectation count.
Is there a better way to go about this, other than doing something like
beStrictAboutTestsThatDoNotTestAnything="false"
?The text was updated successfully, but these errors were encountered: