簡體   English   中英

使用Laravel和Mockery進行單元測試REST更新

[英]Unit Testing REST Update with Laravel and Mockery

我似乎無法弄清楚如何對控制器的更新進行單元測試。 我收到以下錯誤:

method update() from Mockery_0_App.... Should be called exactly 1 times but called 0 times.

在刪除更新中的if語句之后(檢查過敏是否存在之后),在將id添加唯一驗證規則的行上出現以下錯誤:

Trying to get property of on object

我的代碼:

控制器:

class AllergyController extends \App\Controllers\BaseController
{
    public function __construct(IAllergyRepository $allergy){
        $this->allergy = $allergy;
    }

    ...other methods (index,show,destroy) ...

    public function update($id)
    {
        $allergy = $this->allergy->find($id);

        //if ($allergy != null) {
            //define validation rules
            $rules = array(
                'name' => Config::get('Patient::validation.allergy.edit.name') . $allergy->name
            );

            //execute validation rules
            $validator = Validator::make(Input::all(), $rules);
            $validator->setAttributeNames(Config::get('Patient::validation.allergy.messages'));

            if ($validator->fails()) {
                return Response::json(array('status' => false, 'data' => $validator->messages()));
            } else {
                $allergy = $this->allergy->update($allergy, Input::all());

                if ($allergy) {
                    return Response::json(array('status' => true, 'data' => $allergy));
                } else {
                    $messages = new \Illuminate\Support\MessageBag;
                    $messages->add('error', 'Create failed! Please contact the site administrator or try again!');

                    return Response::json(array('status' => false, 'data' => $messages));
                }
            }
        //}
        $messages = new \Illuminate\Support\MessageBag;
        $messages->add('error', 'Cannot update the allergy!');

        return Response::json(array('status' => false, 'data' => $messages));
    }

}

TestCase:類AllergyControllerTest擴展了TestCase {

public function setUp()
{
    parent::setUp();

    $this->allergy = $this->mock('App\Modules\Patient\Repositories\IAllergyRepository');
}

public function mock($class)
{
    $mock = Mockery::mock($class);

    $this->app->instance($class, $mock);

    return $mock;
}

public function tearDown()
{
    parent::tearDown();
    Mockery::close();
}

public function testIndex()
{
    $this->allergy->shouldReceive('all')->once();

    $this->call('GET', 'api/allergy');

    $this->assertResponseOk();
}

...Other tests for Index and Show ...

public function testUpdate()
{
    $validator = Mockery::mock('stdClass');
    Validator::swap($validator);

    $input = array('name' => 'bar');

    $this->allergy->shouldReceive('find')->with(1)->once();
    $validator->shouldReceive('make')->once()->andReturn($validator);
    $validator->shouldReceive('setAttributeNames')->once();
    $validator->shouldReceive('fails')->once()->andReturn(false);;
    $this->allergy->shouldReceive('update')->once();

    $this->call('PUT', 'api/allergy/1', $input);

    $this->assertResponseOk();
}

}

配置驗證規則文件:

return array(
    'allergy' => array(
        'add' => array(
            'name' => 'required|unique:Allergy'
        ),
        'edit' => array(
            'name' => 'required|unique:Allergy,name,'
        ),
        'messages' => array(
            'name' => 'Name'
        )
    )
);

有沒有一種方法可以模擬提供給驗證規則的值? 或解決此問題的最佳方法是什么?

我將代碼更改為此,現在可以正常工作了! :)

$validator = Mockery::mock('stdClass');
Validator::swap($validator);

$allergyObj = Mockery::mock('stdClass');
$allergyObj->name = 1;

$input = array('name' => 'bar');

$this->allergyRepo->shouldReceive('find')->with(1)->once()->andReturn($allergyObj);
$validator->shouldReceive('make')->once()->andReturn($validator);
$validator->shouldReceive('setAttributeNames')->once();
$validator->shouldReceive('fails')->once()->andReturn(false);;
$this->allergyRepo->shouldReceive('update')->once();

$this->call('PUT', 'api/allergy/1', $input);

$this->assertResponseOk();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM