Impossible to say without sharing the relevant test example, and the application code - i.e. route definition and Controller action.
On the face of it, it seems that you are using an incorrect request verb for the endpoint.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm following the TDD workflow and trying to test Post and resource routing. I have read several posts here and on other sites that have indicated that the csrf function is needed if using a form. I'm not there yet (using Dusk) just phpunit at the moment.
Is there a way to get around this exception error?
Impossible to say without sharing the relevant test example, and the application code - i.e. route definition and Controller action.
On the face of it, it seems that you are using an incorrect request verb for the endpoint.
@jmacdiarmid Laravel handles CSRF automatically in tests so you don’t need to explicitly pass a token.
Can you share an example test case, and the error you’re getting?
For convenience, the CSRF middleware is automatically disabled when running tests.
POST Method is not supported for this route usually meant there is no POST method in the route for that particular url.. double check the route especially typo error
Here's my code:
TestCase:
namespace Tests;
use Faker\Generator;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Faker\Factory as Faker;
/**
* Class TestCase
* @package Tests
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations, DatabaseTransactions;
protected Generator $faker;
/**
* Set up the test
*/
public function setUp(): void
{
parent::setUp();
$this->faker = Faker::create();
}
/**
* Reset the migrations
*/
public function tearDown(): void
{
$this->artisan('migrate:reset');
parent::tearDown();
}
}
ContractCreationTest.php
namespace Tests\Feature;
use App\Models\Contract;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ContractCreationTest extends TestCase
{
/** @test */
public function a_contract_can_be_added_to_the_database(): void
{
$this->withoutExceptionHandling();
// Tried this first
// $response = $this->post('admin/contracts/create', [
// 'company_name' => 'ABC Candle Company, Inc.',
// ]);
$response = $this->call('POST','admin/contracts/create', [
'company_name' => 'ABC Candle Company, Inc.',
]);
$response->assertOk();
self::assertCount(1, Contract::all());
}
}
Generally the create endpoint is the view with the form, rather than the endpoint that the form POSTs to; is this the case with your app (you didn't share the route definition as requested)?
More likely, the URI is admin/contracts:
$response = $this->post('admin/contracts', [
'company_name' => 'ABC Candle Company, Inc.',
]);
Ok, that explains it. My misunderstanding. :)
My intent was to test the backend. I was going to use Dusk and / or Livewire for the frontend.
Here's the route I was using:
Route::post('contracts', [App\Http\Controllers\Admin\ContractController::class, 'store'])->name('contracts.store');
If you are using route names, then it might be useful to consider using them both in the form action, and in the test examples. It will express the intent more clearly:
$response = $this->post(route('contracts.store'), [
'company_name' => 'ABC Candle Company, Inc.',
]);
I would suggest you consider Dusk only if there are Javascript behaviours/side-effects that you want to test. Feature and Unit tests using the general TestCase should cover much of your Laravel application.
Please or to participate in this conversation.