PHPUnit 错误:给定数据未能通过验证
Posted
技术标签:
【中文标题】PHPUnit 错误:给定数据未能通过验证【英文标题】:PHPUnit error: the given data failed to pass validation 【发布时间】:2018-07-07 15:45:36 【问题描述】:所以我一直在关注 Laracasts 视频,在那里我上了第 10 课。我密切关注该视频,但我的验证无法通过。当我运行 phpUnit 时,我收到以下错误:
:
1) CreateThreadsTest::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/CreateThreadsTest.php:68 /home/vagrant/Code/forum/tests/Feature/CreateThreadsTest.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;
这是我的 CreateThreadsTest.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】:尝试这样做...
TestCase.php 文件:
protected function setUp()
parent::setUp();
$this->disableExceptionHandling();
$this->withExceptionHandling();
CreateThreadsTest.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);
【讨论】:
以上是关于PHPUnit 错误:给定数据未能通过验证的主要内容,如果未能解决你的问题,请参考以下文章
phpunit 测试返回 302 验证错误,为啥不返回 422
在'identityPoolId'处检测到的验证错误未能满足约束
Spring security oauth2 在 30 秒后未能通过颁发者验证