생성자를 통해 Eloquent 모델을 주입 한 저장소를 테스트해야합니다.Laravel - Testing Repositories
class EloquentOrderRepository implements OrderRepositoryInterface
{
protected $model;
public function __construct(Order $model)
{
$this->model = $model;
}
public function calculateValues(array $deliveryOption = null)
{
if (! is_null($deliveryOption)) {
$this->model->value_delivery = (float) number_format($deliveryOption['price'], 2);
}
$this->model->value_products = (float) number_format($this->model->products->getTotal(), 2);
$this->model->value_total = (float) $this->model->value_products + $this->model->value_delivery;
}
}
$this->model->value_products
(또는 속성 중 하나)을 호출 할 때 문제가 발생합니다. Eloquent 모델은 조롱 된 모델에없는 setAttribute
메서드를 호출하려고합니다. 이 메서드를 조롱하면 속성을 올바르게 설정할 수 없으며 테스트 어설 션이 실패합니다. 이에
<?php
class EloquentOrderRepositoryTest extends \PHPUnit_Framework_TestCase
{
protected $model, $repository;
public function setUp()
{
$this->model = Mockery::mock('Order');
}
public function test_calculate_values()
{
$repository = new EloquentOrderRepository($this->model);
$this->model->products = m::mock('SomeCollection');
$this->model->products->shouldReceive('getTotal')->once()->withNoArgs()->andReturn(25);
$this->model->calculateValues(array('price' => 12));
$this->assertEquals(12, $this->model->value_delivery);
$this->assertEquals(25, $this->model->value_products);
$this->assertEquals(37, $this->model->value_total);
}
}
어떤 생각 : 여기
내 시험입니까?
물론 합리적입니다. 답변 해주셔서 감사합니다. –