문제

나는 모델을 테스트하고, 그 테스트 중 하나를 테스트하고 싶습니다. 제가 테스트하는 모델의 방법을 조롱하고 싶습니다. 그래서 저는 컨트롤러를 테스트하지 않으며 동일한 모델의 한 가지 방법 만 대체하고 싶지 않습니다.

이유는이 모델 메소드가 파일 업로드 핸들러를 호출한다는 것입니다. 이 기능은 이미 다른 곳에서 테스트되었습니다.

내가 지금하고있는 것은 다음과 같습니다. 모델 '콘텐츠'를 테스트합니다. 거기에서 'Sendteaser'를 호출하는 'Addteaser'메소드의 방법을 테스트합니다. 그래서 저는 Sendteaser를 모의하고 납땜하는 동안 SendTeaser의 방법에 대한 성공적인 답변을 가동하고 싶습니다.

다음과 같습니다 :

    $model = $this->getMock('Content', array('sendTeaser'));
    $model->expects($this->any())
    ->method('sendTeaser')
    ->will($this->returnValue(array('ver' => ROOT.DS.APP_DIR.DS.'webroot/img/teaser/5/555_ver.jpg')));


    $data = array(
        'Content' => array(
            'objnbr' => '555',
            'name' => '',
             ...
            )
        )
    );
    $result = $model->addTeaser($data);
    $expected = true;
    $this->assertEquals($expected, $result);
.

테스트를 실행하면 'SendTeaser'메소드 내의 모델이 제대로 호출되지 않는 오류가 발생합니다. 야! 그것은 불리우해서는 안됩니다! 나는 그 방법을 조롱했다! ..... 아닌가?

방법을 조롱하는 것에 대한 적절한 구문은 무엇입니까?

항상 도움을주는 것처럼 많은 감사합니다!

CALAMITY JANE

편집 : 다음은 내 모델에 대한 관련 코드입니다.

    App::uses('AppModel', 'Model');
    /**
    * Content Model
    *
    * @property Category $Category
    */
    class Content extends AppModel {

    public $dateipfad = '';
    public $fileName = '';
    public $errormessage = '';
    public $types = array(
        'sqr' => 'square - more or less squarish',
        'hor' => 'horizontal - clearly wider than high',
        'lnd' => 'landscape - low but very wide',
        'ver' => 'column - clearly higher than wide',
    );
    public $order = "Content.id DESC";
    public $actsAs = array('Containable');

    public $validateFile = array(
        'size' => 307200,
        'type' => array('jpeg', 'jpg'),
    );


    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $hasMany = array(
        'CategoriesContent' => array(
        'className' => 'CategoriesContent',
        ),
        'ContentsTag' => array(
        'className' => 'ContentsTag',
        ),
        'Description' => array(
        'className'  => 'Description',
        )
    );





    /**
    * Saves the teaser images of all formats.
    *
    * @param array $data
    *
    * @return Ambigous <Ambigous, string, boolean>
    */
    public function addTeaser($data)
    {
        $objnbr = $data['Content']['objnbr'];
        $type = $data['Content']['teaser-type'];

        if (!empty($data['Content']['teaser-img']['tmp_name'])) {
        $mFileNames = $this->sendTeaser($data, $objnbr, $type);
        }

        if (!is_array($mFileNames)) {
        $error = $mFileNames;
        //Something failed. Remove the image uploaded if any.
        $this->deleteMovedFile(WWW_ROOT.IMAGES_URL.$mFileNames);
        return $error;
        }
        return true;
    }



    /**
    * Define imagename and save the file under this name.
    *
    * Since we use Imagechache, we don't create a small version anymore.
    *
    * @param integer $objnbr
    * @param string $teasername
    *
    * @return multitype:Ambigous <string, boolean> |Ambigous <boolean, string>
    */
    public function sendTeaser($data, $objnbr, $type)
    {
        //$path = str_replace('htdocs','tmp',$_SERVER['DOCUMENT_ROOT']);
        $this->fileName = $this->getImageName($objnbr, $type);
        $oUH = $this->getUploadHandler($data['Content']['teaser-img']);
        debug($oUH);
        exit;
        $error = $oUH->handleFileUpload();
        if (empty($type))
        $type = 0;
        if ($error === 'none'){
        // Send to ImageChacheServer
        $oICC = $this->getImagecacheConnector();
        $sCacheUrl = $oICC->uploadFile($objnbr, $type, $this->fileName);
        debug($sCacheUrl);
        return array($type => $this->fileName);
        }
        return $error;
    }


    public function getUploadHandler($imgdata)
    {
        App::uses('UploadHandler', 'Lib');
        $oUH = new UploadHandler($this, $imgdata);
        return $oUH;
    }



}
.

getMock을 getMockFormodel로 변경하면 출력이 변경되지 않았습니다.

도움이 되었습니까?

해결책

$ this-> getMock은 조롱하는 방법이 아닙니다.$ this-> 생성

를 사용해야합니다.

i cakephp에 대한 책을 읽으려면 https://leanpub.com/cakephpunittesting

다른 팁

@ndm 케이크 테스트 도우미 클래스 CakeTestCase::getMockForModel()

$theModel = CakeTestCase::getMockForModel('Modelname', ['theMethodToMock']);
$theModel->expects($this->once())
         ->method('theMethodToMock')
         ->will($this->returnValue('valueToReturn'));
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top