Laravel PHPUnit测试未定义的错误变量

时间:2016-08-21 19:07:56

标签: laravel laravel-5 laravel-5.2

我希望有人向我解释当我从Laravel应用程序运行phpunit测试时,为什么我会收到未定义的变量错误。我设置了if语句,以便默认情况下不添加它们,所以不确定原因。

<?php

Route::auth();

Route::group(['middleware' => 'web'], function () {
    Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'HomeController@dashboard']);
});

<form role="form" method="POST" action="{{ url('/login') }}">
    {{ csrf_field() }}
    <div class="form-group form-material floating {{ $errors->has('email') ? 'has-error' : '' }}">
        <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}"/>
        <label for="email" class="floating-label">Email</label>
        @if ($errors->has('email'))
            <small class="help-block">{{ $errors->first('email') }}</small>
        @endif
    </div>
    <div class="form-group form-material floating {{ $errors->has('password') ? 'has-error' : '' }}">
       <input id="password" type="password" class="form-control" name="password" />
       <label for="password" class="floating-label">Password</label>
       @if ($errors->has('password'))
           <small class="help-block pull-left">{{ $errors->first('password') }}</small>
       @endif
   </div>
   <div class="form-group clearfix">
       <div class="checkbox-custom checkbox-inline checkbox-primary checkbox-lg pull-left">
            <input type="checkbox" id="inputCheckbox" name="remember">
            <label for="inputCheckbox">Remember me</label>
        </div>
        <a class="pull-right" href="{{ url('/password/reset') }}">Forgot password?</a>
    </div>
    <button type="submit" class="btn btn-primary btn-block btn-lg margin-top-40">Log in</button>
</form>


<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class LoginTest extends TestCase
{
    use WithoutMiddleware;

    /** @test */
    public function user_can_visit_login_page()
    {
        $this->visit('login');
    }

    /** @test */
    public function user_submits_form_with_no_values_and_returns_errors()
    {
        $this->visit('login')
            ->press('Log in')
            ->seePageIs('login')
            ->see('The email field is required.')
            ->see('The password field is required.');
    }

    /** @test */
    public function it_notifies_a_user_of_wrong_login_credentials()
    {
        $user = factory(App\User::class)->create([
            'email' => 'john@example.com',
            'password' => 'testpass123'
        ]);


        $this->visit('login')
            ->type($user->email, 'email')
            ->type('notmypassword', 'password')
            ->press('Log in')
            ->seePageIs('login');
    }

    public function user_submits_login_form_unsuccesfully()
    {
        $user = factory(App\User::class)->create([
            'email' => 'john@example.com',
            'password' => 'testpass123'
        ]);

        $this->visit('login')
            ->type($user->email, 'email')
            ->type($user->password, 'password')
            ->press('Log in')
            ->seePageIs('dashboard');


    }
}

给出的错误

1) LoginTest::user_can_visit_login_page
A request to [http://myapp.app/login] failed. Received status code [500].

/Users/me/Projects/repositories/MyApp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
/Users/me/Projects/repositories/MyApp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
/Users/me/Projects/repositories/MyApp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:61
/Users/me/Projects/repositories/MyApp/tests/LoginTest.php:13

Caused by
exception 'ErrorException' with message 'Undefined variable: errors' in /Users/me/Projects/repositories/MyApp/storage/framework/views/cca75d7b87e55429621038e76ed68becbc19bc14.php:30
Stack trace:

1 个答案:

答案 0 :(得分:1)

从测试中删除 WithoutMiddleware 特征。

&#XA;