PHPUnit “Mocked method does not exist.” when using $mock->expects($this->at(…))

前端 未结 7 1471
失恋的感觉
失恋的感觉 2021-02-05 00:56

I\'ve run into a strange issue with PHPUnit mock objects. I have a method that should be called twice, so I\'m using the \"at\" matcher. This works for the first time the method

7条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 01:24

    As far as i can tell from the Demo code it should work. I produced a working example in case you are running an older PHPUnit Version and want to check that way if it works for you too.

    In case that doesn't help maybe you could provide a bit more (at best executable) code ? :)

    getMock('MyClass');
            $mock->expects($this->at(0))
                 ->method('exists')
                 ->with($this->equalTo('foo'))
                 ->will($this->returnValue(true));
    
            $mock->expects($this->at(1))
                 ->method('exists')
                 ->with($this->equalTo('bar'))
                 ->will($this->returnValue(false));
    
            $this->assertTrue($mock->exists("foo"));
            $this->assertFalse($mock->exists("bar"));
        }
    
    }
    
    class MyClass {
    
        public function exists($foo) {
            return false;
        }
    }
    

    printing

    phpunit MyTest.php
    PHPUnit 3.4.15 by Sebastian Bergmann.
    
    .
    
    Time: 0 seconds, Memory: 4.25Mb
    
    OK (1 test, 3 assertions)
    

提交回复
热议问题