두 클래스가 있는데, PHPUnit을 통해 테스트하고 싶습니다. 하지만 나는 물건을 조롱 할 때 잘못했다. 첫 번째 클래스에서 호출 한 메서드를 변경하고 싶습니다.모의를 사용하여 PHPUnit 모의
class One {
private $someVar = null;
private $abc = null;
public function Start() {
if (null == $this->someVar) {
$abc = (bool)$this->Helper();
}
return $abc;
}
public function Helper() {
return new Two();
}
}
Class Two {
public function Check($whateverwhynot) {
return 1;
}
}
Class testOne extends PHPUnit_Framework_TestCase {
public function testStart() {
$mockedService = $this
->getMockBuilder(
'Two',
array('Check')
)
->getMock();
$mockedService
->expects($this->once())
->method('Check')
->with('13')
->will($this->returnValue(true));
$mock = $this
->getMockBuilder(
'One',
array('Helper'))
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->once())
->method('Helper')
->will($this->returnValue($mockedService));
$result = $mock->Start();
$this->assertFalse($result);
}
}
그리고 $result
대신 'true
'의, NULL
이다의 결과는
:
F
Time: 0 seconds, Memory: 13.00Mb
There was 1 failure:
1) testOne::testStart
Expectation failed for method name is equal to <string:Check> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
아이디어 ?
업데이트 - 환경 : PHP의 5.4.3x, phpunit을 3.7.19 - 중요한 것은 : 당신은 ouzo-goodies에서 모의 객체를 사용하여 원래의 클래스 (클래스 하나와 클래스 두)
PHPUnit은 의존성 주입을 위해 작동합니다. 조롱 된 두 클래스를 하나에 주입해야합니다. 또한 getMockBuilder 메소드의 매개 변수를 올바르게 사용하지 않았습니다. – SenseException
어디서 getMockBuilder를 잘못 사용합니까? 두 번째 mocked 클래스를 첫 번째 클래스에 삽입하려면 어떻게해야합니까? 어떤 예제 나 문서 또는 그 어떤 것도 발견하지 못했습니다. 링크 나 뭔가를 놓을 수 있습니까? – huncyrus