如何在 Laravel 单元测试期间获取视图数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21139026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get view data during unit testing in Laravel
提问by Claire
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?
我想检查在控制器函数中提供给视图的数组是否具有某些键值对。我如何使用 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');
}
回答by Claire
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.
我找到了一个更好的方法来做到这一点。我在 TestCase 中编写了一个函数,它从视图数据中返回我想要的数组。
protected function getResponseData($response, $key){
$content = $response->getOriginalContent();
$content = $content->getData();
return $content[$key]->all();
}
So to get a value from the $data object I simply use $user = $this->getResponseData($response, 'user');
所以为了从 $data 对象中获取一个值,我只是使用 $user = $this->getResponseData($response, 'user');
回答by cmac
Inside a test case use:
在测试用例中使用:
$data = $this->response->getOriginalContent()->getData();
$data = $this->response->getOriginalContent()->getData();
Example:
例子:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
// do your tests on the data
}
}
Example dumping data so you can see what in data(array) passed to view:
示例转储数据,以便您可以查看传递给视图的数据(数组)中的内容:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
dd($data);
}
}
Should get something back like what's in image:
应该得到一些像图像中的东西:
回答by berrberr
So looking at how assertViewHasis implemented HEREit looks like, what the method does, is access the view's data after this call:
所以看看这里assertViewHas是如何实现的,方法的作用是在这个调用之后访问视图的数据:
$response = $this->client->getResponse()->original;
In your code, the line:
在您的代码中,该行:
$response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);
essentially returns the same thing as the line above it, namely a \Illuminate\Http\Response(which extends the symfony component \HttpFoundation\Response)
基本上返回与其上方的行相同的内容,即 a \Illuminate\Http\Response(扩展 symfony 组件\HttpFoundation\Response)
So, inside the assertViewHasfunction it looks like laravel accesses the data using $response->$key, so I would try to access the clientsand 'content' variables through the $responseobject.
因此,在assertViewHas函数内部,看起来 laravel 使用 访问数据$response->$key,所以我会尝试clients通过$response对象访问和 'content' 变量。
If that doesn't work try searching around the TestCasefile in the Laravel framework ... I'm sure the answer is in there somewhere. Also try to dump the $responseobject and see what it looks like, there should be some clues there.
如果这不起作用,请尝试在TestCaseLaravel 框架中搜索文件……我相信答案就在某个地方。也尝试转储$response对象,看看它是什么样子,那里应该有一些线索。
The first thing I would try, though, is accessing your data through the $responseobject.
不过,我要尝试的第一件事是通过$response对象访问您的数据。
回答by Claire
I managed it by doing it in a messy way. I used assertViewHas:
我通过以凌乱的方式进行管理。我用过assertViewHas:
$this->assertViewHas('clients', array('1' => 'Client 1', '6' => 'Client2'));
回答by MURATSPLAT
You can access data in the response and it can be checked..
您可以访问响应中的数据并且可以检查它。
public function testSimpleLastProducts() {
$res = $this->call('GET', '/');
$this->assertResponseOk();
$this->assertViewHas('lastProducts');
$lastProductOnView = $res->original['lastProducts'];
$this->assertEquals(6, count($lastProductOnView));
}
回答by Cesar Martinez Dominguez
Looking for something like this in 2019, I got:
$response->getData()->data[0]->...my properties
在 2019 年寻找这样的东西,我得到了:
$response->getData()->data[0]->...my properties
Still looking for a simpler way to access it.
仍在寻找一种更简单的方法来访问它。
回答by coderama
This worked for me:
这对我有用:
$response->getSession()->get("errors")
And from there you can check the contents of the message box for whatever error you might want verify.
从那里您可以检查消息框的内容是否有您可能想要验证的错误。


