asadali007's avatar

HTTP test

Hello Everyone ; i am running this test

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

        $response->assertStatus(200)
            ->assertSeeText('Type')
            ->assertViewIs('rooms.index')
            ->assertViewHas('rooms');
    }

i got this error

 • Tests\Feature\ShowRoomsControllerTest > example
  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

  at tests/Feature/ShowRoomsControllerTest.php:20
     16▕     public function test_example()
     17▕     {
     18▕         $response = $this->get('/rooms');
     19▕ 
  ➜  20▕         $response->assertStatus(200)
     21▕             ->assertSeeText('Type')
     22▕             ->assertViewIs('rooms.index')
     23▕             ->assertViewHas('rooms');
     24▕     }


  Tests:  1 failed
  Time:   0.23s

0 likes
9 replies
frankielee's avatar

Because the API /rooms/ returns 500 internal server error, not 200 status code.

Tray2's avatar

Check your RoomsController@index and your rooms.index view since it's there the problem is.

CorvS's avatar

And what exactly is your question here? Check your logs in order to find what causes the 500 error.

asadali007's avatar

@tray2 THIS is showroomscontroller

<?php

namespace App\Http\Controllers;
use App\Models\Room;
use App\Models\RoomType;
use Illuminate\Http\Request;
use Illuminate\Queue\NullQueue;
use Illuminate\Support\Facades\DB;

class ShowRoomsController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request, $roomType= null )
    {
       if(isset($roomType))
      {
          $rooms = Room::where('room_type_id','!=',$roomType)->get();   
      }
      else
       {
          $rooms = Room::get();
       }
      
        // values of key rooms is going to be our rooms variable
      return view('rooms.index',['rooms' => $rooms]);
        
       // http://127.0.0.1:8888/rooms/1
        //

        // one to one relationship 
     /*  $room = RoomType::find(1)->room;
       echo '<pre>';
       echo $room;
       */
    }
}

asadali007's avatar

this is my route

Route::get('/rooms/{roomType?}',ShowRoomsController::class);

bugsysha's avatar

Add this above $response = $this->get('/rooms'); and post the issue here.

$this->withoutExceptionHandling();
asadali007's avatar

Now I make a new test file in tests\Feature UserTest.php and i make the route

Route::get('/rooms/{roomType?}',ShowRoomsController::class);

but i still got same error

 • Tests\Feature\UserTest > example
  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

  at tests/Feature/UserTest.php:21
     17▕     {
     18▕         $response = $this->get('/rooms');
     19▕         $this->assertTrue(true);
     20▕ 
  ➜  21▕         $response->assertStatus(200);
     22▕             
     23▕     
     24▕     }
     25▕ }


  Tests:  1 failed, 17 passed
  Time:   2.93s

this is my test file

<?php

namespace Tests\Feature;

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

class UserTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/rooms');
        $this->assertTrue(true);

        $response->assertStatus(200);
            
    
    }
}

SilenceBringer's avatar

@asadali007 check what @bugsysha suggest. Change your test

public function test_example()
    {
        $this->withoutExceptionHandling();

        dd($this->get('/rooms')->getContent());    
    }

Please or to participate in this conversation.