Php 使用Laravel和Mockry进行单元测试REST更新

Php 使用Laravel和Mockry进行单元测试REST更新,php,unit-testing,laravel,mockery,Php,Unit Testing,Laravel,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 e

我似乎不知道如何对控制器的更新进行单元测试。我得到以下错误:

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

}
测试用例: 类AllergyController测试扩展了TestCase {

}

配置验证规则文件:

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

有没有办法实际模拟验证规则中提供的值?或者,解决这个问题的最佳方法是什么?

我将代码改成了这个,现在它可以工作了!:)

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();