AppDividend
Latest Web Development Tutorials

Laravel 5.5 HTTP Testing Example From Scratch

Laravel 5.5 Testing

1,502

Laravel 5.5 HTTP Testing Example From Scratch is today’s topic. Testing an HTTP request is a very important task as a backend developer to verify that our project works fine. Gladly, Laravel provides a very fluent API to test the HTTP request.

If you are new to testing the checkout my below article.

Laravel 5.5 Testing With Example

Laravel 5.5 HTTP Testing

We are learning Laravel HTTP Testing with an example, so first make one copy of a Laravel project.

Step 1: Make one API route to test.

Go to the routes  >>  web.php file and register this route.

<?php

// web.php

Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

Step 2: Make one test file.

In your terminal, type the following command in it.

php artisan make:test HTTPTest

Now, this file has a testExample() function by default. We need to add the following code in it.

public function testExample()
{
   $response = $this->get('/getdata');
   $response->assertJson(['website'=>'AppDividend']);
}

So it is the example of an assertJson function.

$response>assertJson(array $data)

This function basically checks the return response from the original route. In the web.php file, we are returning following JSON response.

// web.php

Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

And in code, we are also returning the same array response.

Now, run the test by typing following command.

vendor/bin/phpunit

or

phpunit

You will see all the tests will pass. Now, change the JSON response in original web.php route.



Related Posts
1 of 10

Now, run the test again and it will fail. So we are testing the response of an API point. So this is one kind of HTTP testing.

$response>assertViewIs($value)

This function basically checks the return view from either web.php file or particular controller file.

For example, let us test the following route.

<?php

// web.php

Route::get('/', function () {
    return view('welcome');
});
Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

Here, in the home route, we are returning the welcome view, which is welcome.blade.php inside resources  >>  views folder.

Now, write the another test for it inside HTTPTest.php file.

We just need to create another function inside HTTPTest.php file and name it whatever you want it.

<?php

// HTTPTest.php

namespace Tests\Feature;

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

class HTTPTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
      $response = $this->get('/getdata');
      $response->assertJson(['website'=>'AppDividend']);
    }

    public function testView()
    {
      $response = $this->get('/');

      $response->assertViewIs('welcome');
    }
}

Here, I have named it like testView() because I need to test the return view from the web API point.

Now, again run the test and you will pass all the tests and if you change the view name and again run the test, it will surely fail.

$response>assertStatus($code)

This function checks the status code return from the web API point. If we request the route which returns another response code and our test returns the another test code then the test will fail otherwise both returns same status code then our test passes.

In the HTTPTest.php file, make the following function in it.

// HTTPTesting.php

public function testStatusCode()
{
      $response = $this->get('/status');

      $response->assertStatus(200);
}

And add the route in the web.php file.

// web.php

Route::get('/status', function () {
    abort(404);
});

Both response codes are different so, our test will fail. And if the test expects the same result as route return then it will pass the test.

Our, Final HTTPTest.php file looks like this.

<?php

// HTTPTest.php

namespace Tests\Feature;

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

class HTTPTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
      $response = $this->get('/getdata');
      $response->assertJson(['website'=>'AppDividend']);
    }

    public function testView()
    {
      $response = $this->get('/');

      $response->assertViewIs('welcome');
    }

    public function testStatusCode()
    {
      $response = $this->get('/status');

      $response->assertStatus(200);
    }
}

There are more functions to test the results, which you can get at the following link.

Finally, Laravel 5.5 HTTP Testing Example From Scratch tutorial is over.

Leave A Reply

Your email address will not be published.