PHPUnit错误:给定数据无法通过验证

时间:2018-01-29 00:40:43

标签: php laravel

所以我按照Laracasts视频进行了第10课。我已经密切关注了视频,任何人都无法通过验证。当Irun PHPUnit时,我收到以下错误:

  
    

  
     

1)CreateThreadsT​​est :: a_thread_requires_a_title   Illuminate \ Validation \ ValidationException:给定数据失败   通过验证。

     

/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:105   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:55   /home/vagrant/Code/forum/app/Http/Controllers/ThreadController.php:44   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:55   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:44   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Route.php:203   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Route.php:160   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:559   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:65   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:64   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:59   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:561   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:520   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:498   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:174   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:149   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:234   /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:84   /home/vagrant/Code/forum/tests/Feature/CreateThreadsT​​est.php:68   /home/vagrant/Code/forum/tests/Feature/CreateThreadsT​​est.php:37

这是我的ThreadController.php文件:

<?php

namespace App\Http\Controllers;

use App\Thread;
use Illuminate\Http\Request;

class ThreadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('auth')->except(['index', 'show']);
    }

    public function index()
    {   $threads = Thread::latest()->get();
        return view('threads.index', compact('threads'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('threads.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required'

        ]);
        $thread = Thread::create([
            'user_id' => auth()->id(),
            'title' => request('title'),
            'channel_id' => request('channel_id'),
            'body' => request('body'),

        ]);
        return redirect($thread->path());
    }


    /**
     * Display the specified resource.
     *
     * @param  \App\Thead  $thead
     * @return \Illuminate\Http\Response
     */
    public function show($channelId, Thread $thread)
    {
        return view('threads.show', compact('thread'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Thead  $thead
     * @return \Illuminate\Http\Response
     */
    public function edit(Thead $thead)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Thead  $thead
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Thead $thead)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Thead  $thead
     * @return \Illuminate\Http\Response
     */
    public function destroy(Thead $thead)
    {
        //
    }

}'

这是我的TestCase.php文件:

    <?php
namespace Tests;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    protected function setUp()
    {
        parent::setUp();
        $this->disableExceptionHandling();
    }
    protected function signIn($user = null)
    {
        $user = $user ?: create('App\User');
        $this->actingAs($user);
        return $this;
    }
    // Hat tip, @adamwathan.
    protected function disableExceptionHandling()
    {
        $this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);
        $this->app->instance(ExceptionHandler::class, new class extends Handler {
            public function __construct() {}
            public function report(\Exception $e) {}
            public function render($request, \Exception $e) {
                throw $e;
            }
        });
    }
    protected function withExceptionHandling()
    {
        $this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler);
        return $this;
    }
}

这是我的CreateThreadsT​​est.php文件

    <?php
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class CreateThreadsTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    function guests_may_not_create_threads()
    {

        $this->expectException('Illuminate\Auth\AuthenticationException');

        $this->get('/threads/create')
            ->assertRedirect('/login');
        $this->post('/threads', [])
            ->assertRedirect('/login');
    }

    /** @test */
    function an_authenticated_user_can_create_new_forum_threads()
    {
        $this->withExceptionHandling()->signIn();
        $thread = make('App\Thread');
        $response = $this->post('/threads', $thread->toArray());
        dd($response->headers->get('Location'));

        $this->get($response->headers->get('Location'))
            ->assertSee($thread->title)
            ->assertSee($thread->body);
    }

    /** @test */
    function a_thread_requires_a_title()
    {

        $this->publishThread(['title' => null])->assertSessionHasErrors('title');
//   $this->withExceptionHandling()->signIn();
//
//   $thread = make('App\Thread', ['title' => "A"]);
//
//
//
//   $this->post('/threads', $thread->toArray());
    }

    /** @test */
    function a_thread_requires_a_body()
    {
        $this->publishThread(['body' => null])
            ->assertSessionHasErrors('body');
    }

    /** @test */
    function a_thread_requires_a_valid_channel()
    {
        factory('App\Channel', 2)->create();
        $this->publishThread(['channel_id' => null])
            ->assertSessionHasErrors('channel_id');
        $this->publishThread(['channel_id' => 999])
            ->assertSessionHasErrors('channel_id');
    }

    protected function publishThread($overrides = [])
    {
        $this->withExceptionHandling()->signIn();
        $thread = make('App\Thread', $overrides);
        return $this->post('/threads', $thread->toArray());
    }

}

正如您所看到的,这是我从Laracasts视频中复制的代码。如您所见,即使我为标题启用ExceptionHandling,验证也会在线程控制器上失败。这确实很奇怪。如何正确启用标题的异常处理?感谢?

1 个答案:

答案 0 :(得分:0)

尝试这样做......

TestCase.php文件:

    protected function setUp()
    {
        parent::setUp();
        $this->disableExceptionHandling();
        $this->withExceptionHandling();
    }

CreateThreadsT​​est.php

function an_authenticated_user_can_create_new_forum_threads()
{
    $this->withExceptionHandling()->signIn();
    $thread = make('App\Thread');
    $response = $this->post('/threads', $thread->toArray());

    $response->get($response->headers->get('Location'))
        ->assertSee($thread->title)
        ->assertSee($thread->body);
}