sebastianbergmann/phpunit-mock-objects — Issue #78: No error when method does not exist on class
Author: MarkRoddy
Created: 2012-01-27 22:54:28+00:00
State: open

There are two method calls that accept the names of methods to be mocked or stubbed on a mock object that when given a method that does not exist on the actual object do not result in an error or warning. This makes typos very difficult to diagnoise.

In the code samples below, the ExampleClass has methods foo() and bar().

As seen below, when setting an expectation for a method, there is a typo in the name of the method specified. Ideally this would raise an error and cause the test to fail so it could be quickly corrected. Also good would be if a warning was issued that the method does not exist. Currently, there is no indication that anything is wrong. This at best leads to difficult to diagnose errors, at worst it could lead to tests which pass when they should not.
public function testTypoForMockExpectation() {
$sut = $this->getMockBuilder("ExampleClass")
->setMethods(array("foo"))
->getMock();
$sut->expects($this->once())
->method('foooooooo')
->will($this->returnValue(4));
$this->assertEquals(5, $sut->bar(1));
}

Additionly, the same problem exists when specifying a set of methods to to be set at mock creation time.
public function testTypoWhenCreatingMock() {
$sut = $this->getMockBuilder("ExampleClass_ErrorInMethodNameOverride")
->setMethods(array("fooooooooooooo"))this?
->getMock();
$sut->expects($this->once())
->method('foo')
->will($this->returnValue(4));
$this->assertEquals(5, $sut->bar(1));
}