나는 처음으로 조롱/조롱을 당하고 있으며, 다음 테스트가 실제로 내 코드를 만지고 있는지, 아니면 내가 만든 조롱만을 테스트하고 있는지 확신 할 수 없다. 또한, 나는이 코드가 저장소 패턴과 잘 맞지 않는다는 것을 알고 있습니다.내가 정확하게 조롱을 사용하고 있는지 확실하지 않다면
클래스 :
<?php namespace Acme\Cart\Repositories;
class EloquentCartRepository{
protected $model_name = 'CartModel';
protected $model;
public function __construct($model = null)
{
$this->model = is_null($model) ? new $this->model_name : $model;
}
public function create_visitor_cart($session_id,$type = 'main'){
return $this->create('visitor',$session_id,$type);
}
protected function create($user_type = null,$user_identifier = null,$type = 'main')
{
if(is_null($user_identifier)) throw new \Exception('Cannot create create cart, missing user identifier');
if(is_null($user_type)) throw new \Exception('Cannot create create cart, missing user type');
if($user_type == 'visitor')
{
$this->model->user_session_id = $user_identifier;
}
else
{
$this->model->user_id = $user_identifier;
}
$this->model->type = $type;
$this->model->save();
return $this->model;
}
}
그리고 내 시험 :
/** @test */
public function create_visitor_cart_calls_internal()
{
$model = m::mock('Models\CartModel');
$model->shouldReceive('user_session_id')->with('sess123');
$model->shouldReceive('type')->with('main');
$model->shouldReceive('save')->andReturn($model);
$repository = new EloquentCartRepository($model);
$created_model = $repository->create_visitor_cart('sess123','main');
$this->assertEquals('sess123',$created_model->user_session_id);
$this->assertEquals('main',$created_model->type);
}
이 내 클래스를 테스트 할 수있는 적절한 방법이 있나요? 또는 Mockery/mocking의 잘못된 사용입니까?