php - PHPUnit:测试 POST 请求
问题描述
我想测试 POST 请求。我的控制器的方法如下所示:
public function store(Request $request)
{
$this->Validate($request, [
'name' => 'required|string|min:5'
]);
$product = Product::create([
'name' => $request->name
]);
return redirect()->back();
}
所以,我写了这个简单的测试,但我有一个错误,因为它收到了 302 代码:
$response = $this->post('/product/store', [
'name' => 'Hello'
])
->assertStatus(201);
我认为这个问题是因为我在存储数据后重定向页面。我如何测试这个 POST 请求?
解决方案
对于重定向,您可以断言状态为 302 ( https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection )
$response = $this->post('/product/store', [
'name' => 'Hello'
])
->assertStatus(302);
推荐阅读
- python - 追加 DataFrame 并在 for 循环中添加条件
- javascript - A-FRAME 不同的组件同时被调用
- c# - 在 C# 中使用 ObRegisterCallbacks
- python - 如何预测时间序列中的时间特征?
- javascript - NextJS:TypeError:无法读取未定义的属性“json”
- java - RealmRecyclerViewAdapter 中的过滤不会隐藏排除的元素
- sql - PostgreSQL:如果不存在则插入,如果存在则返回 ID
- mongodb-query - 有没有其他方法可以在 MongoDB atlas 中获取聚合查询执行时间指标?
- java - 按下按钮时如何在java中只运行一次?
- email - 可以通过 URL 从 Google 发送邮件吗?