How to test POST routes in Laravel

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

I'm doing the following to test a POST call to Laravel. I'm expecting that POST to questions, in accordance with my routes, will be dispatches as the store action method. This works in the browser., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Thanks for contributing an answer to Stack Overflow!,Then if you want to test that the errors also being displayed correctly back. Run again above test with some changes as per below:

You could try this:

public
function testStoreAction() {
   Session::start();
   $response = $this - > call('POST', 'questions', array(
      '_token' => csrf_token(),
   ));
   $this - > assertEquals(302, $response - > getStatusCode());
   $this - > assertRedirectedTo('questions');
}
load more v
88%

I'm doing the following to test a POST call to Laravel. I'm expecting that POST to questions, in accordance with my routes, will be dispatches as the store action method. This works in the browser., How to test POST routes in Laravel ,How can I obtain a list of all files in a public folder in laravel? Dec 8, 2020 ,How to get all the users except current logged in user in laravel eloquent? Dec 8, 2020

My test:

public
function setUp() {
   parent::setUp();

   Session::start();
}

public
function testStoreAction() {
   $response = $this - > call('POST', 'questions', array(
      '_token' => csrf_token(),
   ));

   $this - > assertRedirectedTo('questions');
}
load more v
72%

Often, your route will return a JSON response that contains multiple items, such as multiple users:,Sometimes, your application's routes will return JSON collections that are assigned named keys:,For example, if the JSON response returned by your application contains the following data:,Sometimes, JSON responses returned by your application may contain arrays of objects:

Laravel provides a very fluent API for making HTTP requests to your application and examining the responses. For example, take a look at the feature test defined below:

< ? php

namespace Tests\ Feature;

use Illuminate\ Foundation\ Testing\ RefreshDatabase;
use Illuminate\ Foundation\ Testing\ WithoutMiddleware;
use Tests\ TestCase;

class ExampleTest extends TestCase {
   /**
    * A basic test example.
    *
    * @return void
    */
   public
   function test_a_basic_request() {
      $response = $this - > get('/');

      $response - > assertStatus(200);
   }
}
load more v
65%

Search Answer Titles

Route::get('user/profile', [UserProfileController::class, 'show']) - > name('profile');
load more v
75%

Since we're offering a Post model, let's add some RESTful routes. To keep things simple, we're just going to implement 3 of the RESTful routes:,Let’s verify that we can indeed create a post, show a post and show all posts with our provided routes, views, and controllers.,This section will cover how to provide these type of Blade components in your package.,In the above default configuration, all routes defined in routes.web need to be prefixed with /blogger. In this way, collision with potentially existing routes is avoided.

// 'src/Http/Controllers/Controller.php'
<
? php

namespace JohnDoe\ BlogPackage\ Http\ Controllers;

use Illuminate\ Foundation\ Bus\ DispatchesJobs;
use Illuminate\ Routing\ Controller as BaseController;
use Illuminate\ Foundation\ Validation\ ValidatesRequests;
use Illuminate\ Foundation\ Auth\ Access\ AuthorizesRequests;

class Controller extends BaseController {
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
load more v
40%

Next, install the application dependencies using the following command:,To do this open the app/Exceptions/Handler.php file and update the render function to match the following:,Writing tests for our application’s endpoints with an expectation that they will fail,In this tutorial, we’ll be taking an application with minimal poor coverage and writing test cases to improve the code quality.

Go ahead and clone the application

git clone https: //github.com/yemiwebby/laravel-phpunit-starter
   cd laravel - phpunit - starter
load more v

Other "undefined-undefined" queries related to "How to test POST routes in Laravel"