如何在 Laravel 单元测试期间获取视图数据

How to get view data during unit testing in Laravel(如何在 Laravel 单元测试期间获取视图数据)
本文介绍了如何在 Laravel 单元测试期间获取视图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to check the array given to a view in a controller function has certain key value pairs. How do I do this using phpunit testing?

//my controller I am testing


public function getEdit ($user_id)
{
    $this->data['user'] = $user = ModelsUser::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 = ModelsUser::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 中写了一个函数,它从视图数据中返回我想要的数组.

I found a better way to do it. I wrote a function in the TestCase which returns the array I want from the view data.

protected function getResponseData($response, $key){

    $content = $response->getOriginalContent();

    $content = $content->getData();

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

}

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

So to get a value from the $data object I simply use $user = $this->getResponseData($response, 'user');

这篇关于如何在 Laravel 单元测试期间获取视图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)