【问题标题】:Dusk does not handle POST requests the same way Laravel does when testing a form submissionDusk 处理 POST 请求的方式与 Laravel 在测试表单提交时的处理方式不同
【发布时间】:2021-07-13 05:52:09
【问题描述】:

在 Dusk 中运行测试时,提交表单会生成一个验证错误,内容为“需要查询字段”。手动输入测试页面时不会出现该错误。

我在处理 POST 请求的控制器方法的第一行添加了dd( $request )。当我手动测试页面时,系统会将请求转储到页面。当我使用 Dusk 测试页面时,我得到一个屏幕截图,显示代码行从未执行:页面重新加载并出现验证错误。

我在表单中搜索了一个名为“查询”的隐藏输入。它不存在。

我已经在控制器类和基类中搜索了任何测试“查询”输入的验证。我没有找到。

任何人都可以指出正确的方向来弄清楚为什么页面在自动化测试环境中无法使用serve 命令工作吗?

过去有没有人看到过类似的错误?

【问题讨论】:

    标签: php laravel laravel-dusk


    【解决方案1】:

    简短回答:检查页面以验证选择器是否抓取了正确的表单。在这种情况下,测试人员忘记了菜单栏中存在一个表单。测试是单击菜单栏中的按钮,而不是主页面内容中的按钮。

    原文: 我想有时你只需要走开,回到问题上来。我太专注于页面中心的表单,以至于忽略了菜单栏中的表单,该表单具有名称为“查询”的输入。

    我在 Dusk 命令中单击了错误的按钮,因为我的选择器应用于不同表单上的多个按钮。

    【讨论】:

      【解决方案2】:

      例如,我们可以将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,因为它首先验证了请求并将进入函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        相关资源
        最近更新 更多