개발! 딱 깔끔하고 센스있게!

PHP 유닛 "Mocked method" 가 존재 하지 않 습 니 다. $mock - > expects ($this - > at (...) 를 사용 할 때, 본문

개발 스크랩 메모/PHP

PHP 유닛 "Mocked method" 가 존재 하지 않 습 니 다. $mock - > expects ($this - > at (...) 를 사용 할 때,

렉사이 2020. 12. 2. 22:39

나 는 PHP 유닛 모 의 대상 에 대한 이상 한 질문 을 받 았 다.

나 는 두 번 이나 호출 해 야 하 는 방법 이 있어 서 '잇' 매 칭 기 를 사용 합 니 다.

이것 은 첫 번 째 호출 방법 이지 만 어떤 이유 로 두 번 째 호출 할 때 저 는 'Mocked method' 를 얻 었 습 니 다.

나 는 이전에 성냥 개비 로 이런 상황 을 만난 적 이 없다.

제 코드 는 다음 과 같 습 니 다.

class MyTest extends PHPUnit_Framework_TestCase { ... public function testThis() { $mock = $this->getMock('MyClass', array('exists', 'another_method', '...')); $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)); } ... } 

내 가 테스트 를 실행 할 때, 나 는 얻 은 것:

Expectation failed for method name is equal to  when invoked at sequence index 1. Mocked method does not exist. 

내 가 두 번 째 매 칭 기 를 지 웠 다 면, 나 는 잘못 을 받 지 않 았 을 것 이다.

혹시 이거 본 사람 있어 요?

감사합니다.

대답 하 다.

마지막 문 제 는 제 가 어떻게 '잇' 매 칭 기 작업 을 이해 하 느 냐 입 니 다.

한편, 나의 예 는 단원 테스트 중의 상황 을 한 글자 한 마디 한 마디 로 서술 하지 않 았 다.

나 는 "at" matcher 카운터 가 모든 조 회 를 바탕 으로 하 는 인 스 턴 스 라 고 생각한다.

실제로 모든 개체 의 기초 위 에서 일 하 는 것 이다.

보기:

class MyClass { public function exists($foo) { return false; } public function find($foo) { return $foo; } } 

부정 확:

class MyTest extends PHPUnit_Framework_TestCase { public function testThis() { $mock = $this->getMock('MyClass'); $mock->expects($this->at(0)) ->method('exists') ->with($this->equalTo('foo')) ->will($this->returnValue(true)); $mock->expects($this->at(0)) ->method('find') ->with($this->equalTo('foo')) ->will($this->returnValue('foo')); $mock->expects($this->at(1)) ->method('exists') ->with($this->equalTo('bar')) ->will($this->returnValue(false)); $this->assertTrue($mock->exists("foo")); $this->assertEquals('foo', $mock->find('foo')); $this->assertFalse($mock->exists("bar")); } } 

맞습니다:

class MyTest extends PHPUnit_Framework_TestCase { public function testThis() { $mock = $this->getMock('MyClass'); $mock->expects($this->at(0)) ->method('exists') ->with($this->equalTo('foo')) ->will($this->returnValue(true)); $mock->expects($this->at(1)) ->method('find') ->with($this->equalTo('foo')) ->will($this->returnValue('foo')); $mock->expects($this->at(2)) ->method('exists') ->with($this->equalTo('bar')) ->will($this->returnValue(false)); $this->assertTrue($mock->exists("foo")); $this->assertEquals('foo', $mock->find('foo')); $this->assertFalse($mock->exists("bar")); } } 
Comments