Laravel 5 PHPUnit - Invalid JSON was returned from the route

Routes

Route::group(array('prefix' => 'api'), function() { Route::resource('test', 'TestController', array('only' => array('index', 'store', 'destroy', 'show', 'update'))); }); 

controller

 public function store(Request $request) { return response()->json(['status' => true]); } 

Device class

 public function testBasicExample() { $this->post('api/test')->seeJson(['status' => true]); } 

PHPUnit result:

1) ExampleTest::testBasicExample

Invalid JSON was returned from the route.

Perhaps an exception was thrown? Does anyone see a problem?

+5
source share
1 answer

The problem is the CSRF token .

You can disable middleware using the WithoutMiddleware symbol:

 <?php use Illuminate\Foundation\Testing\WithoutMiddleware; class ExampleTest extends TestCase { use WithoutMiddleware; // } 

Or, if you want to disable only middleware for several test methods, you can call the WithoutMiddleware method from the test methods:

 <?php class ExampleTest extends TestCase { /** * A basic functional test example. * * @return void */ public function testBasicExample() { $this->withoutMiddleware(); $this->visit('/') ->see('Laravel 5'); } } 
+4
source

Source: https://habr.com/ru/post/1237640/


All Articles