在测试表单提交时,Dusk 处理 POST 请求的方式与 Laravel 不同
Dusk does not handle POST requests the same way Laravel does when testing a form submission
当 运行 在 Dusk 中测试时,提交表单会生成一个验证错误,内容为 "The query field is required." 使用手动输入测试页面时不会发生此错误。
我将 dd( $request )
添加到处理 POST 请求的控制器方法的第一行。当我手动测试页面时,系统将请求转储到页面。当我使用 Dusk 测试页面时,我得到一张截图,显示代码行从未执行:页面重新加载并出现验证错误。
我在表单中搜索了一个名为 'query.' 的隐藏输入,它不存在。
我已经在控制器 class 和基础 class 中搜索了任何测试 'query' 输入的验证。我找到了 none.
任何人都可以指出正确的方向来弄清楚为什么该页面在使用 serve
命令可以工作时在自动测试环境中不能工作吗?
过去有没有人见过类似的错误?
简短回答:检查页面以验证选择器是否抓取了正确的表单。在这种情况下,测试人员忘记了菜单栏中存在一个表单。测试是单击菜单栏中的按钮而不是主页内容中的按钮。
原文:
我想有时候你只需要走开然后回到问题上。我过于专注于页面中央的表单,以至于忽略了菜单栏中的表单,该表单具有名称为 'query'.
的输入
我用 Dusk 命令点击了错误的按钮,因为我的选择器应用于不同表单上的多个按钮。
例如,我们可以将 Post
Model With PostController.
您的商店功能可能看起来像
public function store(Request $request)
{
Post::create($request->all());
return redirect()->route('post.index')->with('success','PostCreated Successfully');
}
如果您在函数的开头添加 dd
函数,它将起作用,即)dd($request->all());
但如果您使用自定义请求,例如 PostStoreRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'post_name' => 'required',
];
}
/**
* Custom message for validation
*
* @return array
*/
public function messages()
{
return [
'post_name.required' => 'Enter Post Name',
];
}
}
和PostController@store
public function store(PostStoreRequest $request)
{
Post::create($request->all());
return redirect()->route('post.index')->with('success','PostCreated Successfully');
}
即使你在函数的顶部添加 dd
因为它首先验证请求并且它会进入函数。
当 运行 在 Dusk 中测试时,提交表单会生成一个验证错误,内容为 "The query field is required." 使用手动输入测试页面时不会发生此错误。
我将 dd( $request )
添加到处理 POST 请求的控制器方法的第一行。当我手动测试页面时,系统将请求转储到页面。当我使用 Dusk 测试页面时,我得到一张截图,显示代码行从未执行:页面重新加载并出现验证错误。
我在表单中搜索了一个名为 'query.' 的隐藏输入,它不存在。
我已经在控制器 class 和基础 class 中搜索了任何测试 'query' 输入的验证。我找到了 none.
任何人都可以指出正确的方向来弄清楚为什么该页面在使用 serve
命令可以工作时在自动测试环境中不能工作吗?
过去有没有人见过类似的错误?
简短回答:检查页面以验证选择器是否抓取了正确的表单。在这种情况下,测试人员忘记了菜单栏中存在一个表单。测试是单击菜单栏中的按钮而不是主页内容中的按钮。
原文: 我想有时候你只需要走开然后回到问题上。我过于专注于页面中央的表单,以至于忽略了菜单栏中的表单,该表单具有名称为 'query'.
的输入我用 Dusk 命令点击了错误的按钮,因为我的选择器应用于不同表单上的多个按钮。
例如,我们可以将 Post
Model With PostController.
您的商店功能可能看起来像
public function store(Request $request)
{
Post::create($request->all());
return redirect()->route('post.index')->with('success','PostCreated Successfully');
}
如果您在函数的开头添加 dd
函数,它将起作用,即)dd($request->all());
但如果您使用自定义请求,例如 PostStoreRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'post_name' => 'required',
];
}
/**
* Custom message for validation
*
* @return array
*/
public function messages()
{
return [
'post_name.required' => 'Enter Post Name',
];
}
}
和PostController@store
public function store(PostStoreRequest $request)
{
Post::create($request->all());
return redirect()->route('post.index')->with('success','PostCreated Successfully');
}
即使你在函数的顶部添加 dd
因为它首先验证请求并且它会进入函数。