Jared Clemence
Jared Clemence

Reputation: 1065

Dusk does not handle POST requests the same way Laravel does when testing a form submission

When running tests in Dusk, submitting a form generates a validation error that reads "The query field is required." The error does not occur when testing the page with manual input.

I added dd( $request ) to the first line of the controller method that handles the POST request. When I test the page manually, the system dumps the request to the page. When I test the page with Dusk, I get a screenshot that shows the line of code was never executed: The page reloads with the validation error.

I have searched the form for a hidden input with a name of 'query.' It does not exist.

I have searched the controller class and base classes for any validations that test the 'query' input. I have found none.

Can anyone point me in the right direction to figure out why the page does not work in the automated testing environment, when it does work using the serve command?

Has anyone seen a similar error in the past?

Image shows the screen shot after Dusk clicks "Save" and submits the form. Test fails, because an unknown validation occurs before the App calls the controller.

Upvotes: 0

Views: 1375

Answers (2)

ManojKiran
ManojKiran

Reputation: 6351

For example, we can take as Post Model With PostController.

Your store function may look like

public function store(Request $request)
    {
        Post::create($request->all());
        return redirect()->route('post.index')->with('success','PostCreated Successfully');

}

If you add dd function at the begining of the function it will work ie) dd($request->all());

But if you use custom requests, for example 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',
        ];
    }

}

and the PostController@store

public function store(PostStoreRequest $request)
        {
            Post::create($request->all());
            return redirect()->route('post.index')->with('success','PostCreated Successfully');

    }

Even though if you add dd at the top of function because it validated the request first and it will enter into the function.

Upvotes: 0

Jared Clemence
Jared Clemence

Reputation: 1065

Short Answer: Check the page to verify that the selector is grabbing the correct form. In this case, the tester forgot that a form existed in the menu bar. The test was clicking the button in the menu bar instead of in the main page content.

Original Text: I guess sometimes you just need to walk away and come back to the problem. I was so focused on the form at the center of the page that I overlooked the form in the menu bar that has an input with the name 'query'.

I was clicking the wrong button with my Dusk commands, because my selector applied to multiple buttons on separate forms.

Upvotes: 0

Related Questions