//My Test
public function can_delete_book()
{
$book = create(Book::class);
$this->delete(route('books.destroy',$book));
}
//My api.php
Route::middleware(['auth:sanctum'])->group(function () {
Route::apiResource('books', 'BookController');
});
//My controller
public function destroy(Book $book)
When I inspect the $book in the controller, it is just a new book, not the one I passed in?
EDIT: Just noticed that it works for web routes but but not the api ones. Do we not use route model binding with the api routes?
You can pass an array of middlewares that you want to have excluded to the withoutMiddleware method in your test example. This will allow you to only exclude the specific middleware(s) that you did not want to run.
It depends on what the middleware you want disabled does, for example, we have a number of endpoints that have custom headers which are checked in a series of middlewares; but building these headers up for a simple test example is tedious, and noisy, so we disable that middleware for most tests and make a separate test for the middleware itself.
By disabling you are allowing undetected bugs to stay hidden.
In your opinion.
This approach works; our middleware is tested separately. Our functional test examples are concise, readable and are very apparent what exactly is being tested.