Skip to content

Commit

Permalink
Improve test for spy mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 18, 2015
1 parent 526ab19 commit 1af1dc3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tests/MockClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ public function should_allow_to_spy_method_calls()
'foo' => 'bar',
));

$this->assertEquals('bar', $mock->foo());
// Test PHPUnit's internals to check that the spy was registered
$property = new \ReflectionProperty('PHPUnit_Framework_TestCase', 'mockObjects');
$property->setAccessible(true);
$mockObjects = $property->getValue($this);

$this->assertCount(1, $mockObjects);
$this->assertSame($mock, $mockObjects[0]);

// Cannot use @expectedException because PHPUnit has specific behavior for this
try {
$mock->__phpunit_verify();
$this->fail('Exception not thrown');
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->assertContains('Expected invocation at least once but it never occured', $e->getMessage());
}

// Invoke the mock: the test should now pass
$mock->foo();
}
}

1 comment on commit 1af1dc3

@jdreesen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

Please sign in to comment.