Patrick.SE
Patrick.SE

Reputation: 4564

Laravel Test Confuses POST and GET

I have written the following test :

    /** @test */
    public function register_a_new_user_as_a_candidate_with_correct_input()
    {
        $registrationEmail = '[email protected]';
        $this->visit('/')
             ->click('Register')
             ->seePageIs('/')
             ->type($registrationEmail, 'email')
             ->select('candidate', 'user_type')
             ->press('Register')
             ->seePageIs('/')
             ->see('Registration was successful')
             ->seeInDatabase('users', ['email' => $registrationEmail, 'userable_type' => App\Candidate::class]);
    }

When I manually register myself through the form I successfully complete all the steps with no issue.

When I run this test I get :

There was 1 failure:

1) RegistrationTest::register_a_new_user_as_a_candidate_with_correct_input
A request to [http://localhost/register?_token=GNP3xtGrgyKWKfysE6xHd9WYD04FrSbtqb9IWu08&email=dummy%40mail.com&user_type=candidate] failed. Received status code [405].

/home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
/home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
/home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:113
/home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:556
/home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:543
/home/vagrant/Code/staffsite/tests/acceptance/RegistrationTest.php:18

Caused by
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /home/vagrant/Code/staffsite/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:218

I don't have a route that let's a user register through GET. My form uses Ajax to send the request over POST and my html form uses the type POST.

Is my test incorrect?

Here is the form I am using :

<form type="POST" action="register" class="bootstrap-modal-form">
    {{ csrf_field() }}
    <div class="modal-body">
        <div class="form-group">
            <label for="email" class="sr-only">E-Mail Address</label>
            <input name="email" type="email" class="form-control" placeholder="Email Address" required autofocus>
        </div>

        <div class="radio">
          <label>
            <input name="user_type" type="radio" value="candidate" checked>
            I am a candidate looking for work!
          </label>
        </div>

        <div class="radio">
          <label>
            <input name="user_type" type="radio" value="business">
            I am a business interested in hiring candidates.
          </label>
        </div>
    </div>

    <div class="modal-footer">
        <input type="submit" value="Register" class="btn btn-default btn-primary">
        <button class="btn modal-default-button" data-dismiss="modal">Back</button>
    </div>
</form>

Upvotes: 1

Views: 175

Answers (1)

bishop
bishop

Reputation: 39464

I suspect the problem is that your form is defined as type="POST", while the correct declaration is method="POST". Change to:

<form method="POST" action="register" class="bootstrap-modal-form">

Without the method, the form is submitted as a GET. This doesn't cover the stated fact that it works in your browser. Perhaps there is some caching going on?

Upvotes: 1

Related Questions