You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
When a test double, a stub created with getMock(), for instance, is passed from one test to another using the @depends annotation then the test double does not work as expected in the depending test.
Running the tests shown above leads to the output shown below:
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
.F
Time: 78 ms, Memory: 4.00Mb
There was 1 failure:
1) Test::testTwo
Failed asserting that null matches expected 'b'.
/home/sb/Test.php:31
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
The cause of this behavior is the fact that the test double's __phpunit_verify() method (which is invoked by PHPUnit_Framework_TestCase::verifyMockObjects()) unconditionally cleans up the invocation mocker instance ($this->__phpunit_invocationMocker = null;) for performance reasons.
and PHPUnit_Framework_TestCase::verifyMockObjects() could pass false when there are tests that depend on the current one (this information is not available in PHPUnit, only the inverse).
The text was updated successfully, but these errors were encountered:
I just realized that it would not be enough to not clean up the invocation mocker. In addition it would need to be reset as otherwise its expectations would have already been verified.
And while it can make sense to pass stubs from one test to another using @depends it does not make sense to pass mock objects from one test to another. Some users would expect that the mock object's expectation will be verified again (see above) while other users would assume the opposite.
I am closing this issue and will update the documentation to reflect that the current behavior is actually the intended behavior.
For anyone who comes across this thread looking for the cause of the problem similar to mine:
Had a case where the dependent test method was receiving null. However, this appeared to be the result of an exception thrown in the dependency, which passed due to @expectedException. Of course, the return statement was unreachable.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
When a test double, a stub created with
getMock()
, for instance, is passed from one test to another using the@depends
annotation then the test double does not work as expected in the depending test.Running the tests shown above leads to the output shown below:
The cause of this behavior is the fact that the test double's
__phpunit_verify()
method (which is invoked byPHPUnit_Framework_TestCase::verifyMockObjects()
) unconditionally cleans up the invocation mocker instance ($this->__phpunit_invocationMocker = null;
) for performance reasons.This cleanup could be made optional
and
PHPUnit_Framework_TestCase::verifyMockObjects()
could passfalse
when there are tests that depend on the current one (this information is not available in PHPUnit, only the inverse).The text was updated successfully, but these errors were encountered: