php – 如何在Laravel中进行单元测试期间获取视图数据

我想检查给定一个控制器函数中的视图的数组是否具有某些键值对.我如何使用phpunit测试做到这一点?

//my controller I am testing


public function getEdit ($user_id)
{
    $this->data['user'] = $user = \Models\User::find($user_id);

    $this->data['page_title'] = "Users | Edit";

    $this->data['clients'] = $user->account()->firstOrFail()->clients()->lists('name', 'id');

    $this->layout->with($this->data);

    $this->layout->content = \View::make('user/edit', $this->data);
}

//my test
public function testPostEdit (){

    $user = Models\User::find(parent::ACCOUNT_1_USER_1);

    $this->be($user);

    $response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);   

    //clients is an array.  I want to get this 
    //array and use $this->assetArrayContains() or something
    $this->assertViewHas('clients');

    $this->assertViewHas('content');

}
最佳答案
我发现了一种更好的方法.我在TestCase中编写了一个函数,它从视图数据中返回我想要的数组.

protected function getResponseData($response, $key){

    $content = $response->getOriginalContent();

    $content = $content->getData();

   return $content[$key]->all();

}

因此,要从$data对象中获取值,我只需使用$user = $this-> getResponseData($response,’user’);

转载注明原文:php – 如何在Laravel中进行单元测试期间获取视图数据 - 代码日志