php - 在 laravel 中对视图进行单元测试?
问题描述
如何对返回视图的控制器功能进行单元测试。这是我的控制器功能
public function index(Request $request)
{
$data = Data::all();
return view('index',[
'data' => $data
]);
}
这是测试代码
public function testIndex()
{
$response = $this->withoutMiddleware();
$response = $this->get('/user');
$response->assertSuccessful();
}
我试过这个
public function testIndex()
{
$response = $this->withoutMiddleware();
$response = $this->get('/user');
$response = $this->assertContains('data', $response->content());
$response->assertSuccessful();
}
显示错误
错误:在 null 上调用成员函数 assertSuccessful()
任何想法,如何为我的索引控制器功能编写测试用例?
解决方案
您在第一个断言中重新分配了 $response。没必要这样做
我不知道 assertContains 从我的脑海中返回了什么,但我敢打赌它不是 $this。
老实说,我不知道为什么示例首先要为 $response 分配任何内容,除了来自 HTTP 查询的响应。
public function testIndex()
{
$this->withoutMiddleware();
$response = $this->get('/user');
$this->assertContains('data', $response->content());
$response->assertSuccessful();
}
推荐阅读
- rust - Embedded Rust-如何防止为 ISR 生成“额外”代码
- javascript - 当javascript更改值时如何触发addEventListener
- c# - unity 刚体 移动时会向上反弹
- google-chrome - Chrome iPad 的视口高度错误
- python - 最小生成树删除所有额外的边
- python - Python:仅导入特定方法
- c - 为什么 Clang-tidy 在函数调用中没有检测到从 float 到 uint8_t 的缩小转换?
- javascript - 需要把这个对象放到一个数组中,怎么办?
- python - 如何在 3D numpy 数组的第三维中转换列表对象类型?
- github - GitHub CI/CD - 运行提升的命令