2014-03-25 2 views
0

나는 처음으로 조롱/조롱을 당하고 있으며, 다음 테스트가 실제로 내 코드를 만지고 있는지, 아니면 내가 만든 조롱만을 테스트하고 있는지 확신 할 수 없다. 또한, 나는이 코드가 저장소 패턴과 잘 맞지 않는다는 것을 알고 있습니다.내가 정확하게 조롱을 사용하고 있는지 확실하지 않다면

클래스 :

<?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의 잘못된 사용입니까?

답변

0

반환되는 내용을 테스트하는 대신 해당 내용이 저장되었는지 테스트해야합니다. 즉, ->save()이 실행됩니다. ->save()에 대한 기대치는 $model->shouldReceive('save')->andReturn($model);입니다. 이는 코드가 ->save()의 반환 값을 사용하지 않기 때문에 의미가 없습니다.

프로그래밍에서는 일반적으로 두 가지 유형의 메소드 인 명령과 쿼리를 처리합니다. 쿼리는 일부 값을 가져올 수 있으며, 일부 논리를 수행하고 값을 반환 할 수 있습니다. 명령은 일부 값을 가져올 수 있으며 extern 소스 (예 : 데이터베이스)와 통신하여 아무 것도 반환하지 않습니다. 쿼리는 스텁 (stub)되어야합니다 (즉, 호출되는 양에 대한 기대를하지 말고 리턴하는 것에 대해서만 기대해야합니다). 명령은 조롱 받아야합니다. 즉, 명령은 얼마나 많은 (그리고 있다면) 기대를 포함해야합니다. 호출됩니다).

->save() 메서드는 명령입니다.이 메서드는 데이터베이스와 통신합니다. 그래서 그것은 조롱 받아야합니다. 개체를 조롱하려면 ->once() 조롱 방법을 사용하십시오. 그것이 한 번 호출 할 것을 기대 설정 : 이름에도 불구

/** @test */ 
public function create_visitor_cart_calls_internal() 
{ 
    $model = m::mock('Models\CartModel'); 
    $model->shouldReceive('save')->once(); 

    $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); 
} 

을 조롱는 기본적으로 스터 빙 프레임 워크입니다. 그것은 당신이 명시 적으로 자세한 내용은 ->once()

같은 기대를 지정하지 않는 메소드가 호출되는 것을 확인 워드 프로세서를 참조하지 않습니다 https://github.com/padraic/mockery-docs/blob/master/reference/expectations.rst

관련 문제