Skip to content

Commit 1af1dc3

Browse files
committed
Improve test for spy mocks
1 parent 526ab19 commit 1af1dc3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

tests/MockClassTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public function should_allow_to_spy_method_calls()
114114
'foo' => 'bar',
115115
));
116116

117-
$this->assertEquals('bar', $mock->foo());
117+
// Test PHPUnit's internals to check that the spy was registered
118+
$property = new \ReflectionProperty('PHPUnit_Framework_TestCase', 'mockObjects');
119+
$property->setAccessible(true);
120+
$mockObjects = $property->getValue($this);
121+
122+
$this->assertCount(1, $mockObjects);
123+
$this->assertSame($mock, $mockObjects[0]);
124+
125+
// Cannot use @expectedException because PHPUnit has specific behavior for this
126+
try {
127+
$mock->__phpunit_verify();
128+
$this->fail('Exception not thrown');
129+
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
130+
$this->assertContains('Expected invocation at least once but it never occured', $e->getMessage());
131+
}
132+
133+
// Invoke the mock: the test should now pass
134+
$mock->foo();
118135
}
119136
}

0 commit comments

Comments
 (0)