Skip to content
This repository

No error when method does not exist on class #78

Closed
MarkRoddy opened this Issue · 1 comment

2 participants

MarkRoddy Jeff Welch
MarkRoddy

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"))
            ->getMock();
        $sut->expects($this->once())
            ->method('foo')
            ->will($this->returnValue(4));
        $this->assertEquals(5, $sut->bar(1));
    }
Jeff Welch
Collaborator

This is covered by #12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.