Laravel测试混淆POST和GET

时间:2016-07-21 16:18:25

标签: laravel laravel-5.2

我写了以下测试:

    /** @test */
    public function register_a_new_user_as_a_candidate_with_correct_input()
    {
        $registrationEmail = 'dummy@mail.com';
        $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]);
    }

当我通过表单手动注册时,我成功地完成了所有步骤而没有任何问题。

当我运行此测试时,我得到:

有1次失败:

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

我没有让用户通过GET注册的路由。我的表单使用Ajax通过POST发送请求,我的html表单使用POST类型。

我的测试不正确吗?

以下是我正在使用的表单:

<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>

1 个答案:

答案 0 :(得分:1)

我怀疑问题是您的表单定义为type="POST",而the correct declarationmethod="POST"。改为:

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

如果没有method,表单会以GET的形式提交。这并不包括它在您的浏览器中工作的陈述事实。也许有一些缓存正在进行?