duanchi8836 2015-01-08 23:41
浏览 72

使用Laravel和Mockery对REST更新进行单元测试

I can't seem to figure out how I unit test the update of my controller. i'm getting the following error:

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

After I remove the if statement in the update (after checking if the allergy exists), I get the following error on the line where I add the id the the unique validation rule:

Trying to get property of on object

My Code:

Controller:

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: class AllergyControllerTest extends 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();
}

}

Config validation rules file:

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

Is there a way to actually mock the value provided into the validation rule? Or what is the best way to solve this?

  • 写回答

1条回答 默认 最新

  • duanhe0817825 2015-01-09 13:30
    关注

    I changed my code to this and now it works! :)

    $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();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器