컨트롤러에서 체인 콜을 제대로 Eloquent 모델로 모의하려고합니다. 내 컨트롤러에서는 의존성 삽입을 사용하여 모델에 액세스하여 모의하기 쉽도록해야하지만 체인 된 호출을 테스트하고 올바르게 작동하는 방법을 모르겠습니다. 이것은 PHPUnit과 Mockery를 사용하는 Laravel 4.1의 모든 것입니다.모의 실험에서 체인 메소드 호출 테스트
컨트롤러 :
<?php
class TextbooksController extends BaseController
{
protected $textbook;
public function __construct(Textbook $textbook)
{
$this->textbook = $textbook;
}
public function index()
{
$textbooks = $this->textbook->remember(5)
->with('user')
->notSold()
->take(25)
->orderBy('created_at', 'desc')
->get();
return View::make('textbooks.index', compact('textbooks'));
}
}
컨트롤러 검사 :
<?php
class TextbooksControllerText extends TestCase
{
public function __construct()
{
$this->mock = Mockery::mock('Eloquent', 'Textbook');
}
public function tearDown()
{
Mockery::close();
}
public function testIndex()
{
// Here I want properly mock my chained call to the Textbook
// model.
$this->action('GET', '[email protected]');
$this->assertResponseOk();
$this->assertViewHas('textbooks');
}
}
내가 시험에서 $this->action()
호출하기 전에이 코드를 배치하여이를 달성하기 위해 노력했습니다.
$this->mock->shouldReceive('remember')->with(5)->once();
$this->mock->shouldReceive('with')->with('user')->once();
$this->mock->shouldReceive('notSold')->once();
$this->app->instance('Textbook', $this->mock);
그러나이 결과는 Fatal error: Call to a member function with() on a non-object in /app/controllers/TextbooksController.php on line 28
입니다.
나는 또한 트릭을 할 수 있기를 바래 끈적 인 대안을 시도했다.
$this->mock->shouldReceive('remember')->with(5)->once()
->shouldReceive('with')->with('user')->once()
->shouldReceive('notSold')->once();
$this->app->instance('Textbook', $this->mock);
이 연쇄 된 메서드 호출을 Mockery로 테스트 할 때 최선의 방법은 무엇입니까?
설명서를 읽어 보시기 바랍니다 https://github.com/padraic/mockery#mocking-demeter-chains-and-fluent -interfaces – Shakil