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]);
}
Unit 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? Anyone see the Problem?
The problem is the CSRF Token.
You could disable the middleware by using the WithoutMiddleware
trait:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
class ExampleTest extends TestCase
{
use WithoutMiddleware;
//
}
Or, if you would like to only disable middleware for a few test methods, you may call the withoutMiddleware
method from within 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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With