asyadiqin
asyadiqin

Reputation: 1667

PHPUNIT Test - Expected Status 200 But Received 500

I keep getting status 500 when 200 is expected. These are the files:

ExampleTest.php

namespace Tests\Feature;

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

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

        $response->assertStatus(200);
    }
}

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>
    </php>
</phpunit>

This is the result returned

PHPUnit 7.4.0 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 1.86 seconds, Memory: 20.00MB

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 500.
Failed asserting that false is true.

/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:133
/var/www/html/tests/Feature/ExampleTest.php:19

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

This is the Laravel log file

[2018-10-15 20:56:09] testing.ERROR: Call to a member function load() on null (View: /var/www/html/resources/views/app.blade.php) {"exception":"[object] (ErrorException(code: 0): Call to a member function load() on null (View: /var/www/html/resources/views/app.blade.php) at /var/www/html/app/Ezybyz/Configuration/ProvidesScriptVariables.php:46, Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function load() on null at /var/www/html/app/Ezybyz/Configuration/ProvidesScriptVariables.php:46)

app.blade.php

@php
$config = array_merge(
    Ezybyz::scriptVariables(), [
        // Add key and value here if you want to added to initial state
    ]
)
@endphp

@extends('layouts.main')

@push('meta')
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<meta name="csrf-token" content="{{ csrf_token() }}">
{{-- Fix for chunk error in Webpack and Vue-Router --}}
<base href="/" />
@endpush

@push('favicon')
<link rel="shortcut icon" href="{{ asset('favicon.ico?v=2') }}" type="image/x-icon" />
@endpush

@push('css')
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
@endpush

@push('header_js')
{{--  EzyByz initial state provider --}}
<script>
  window.App = @json($config)
</script>
{{-- Add whitelisted routes for making API calls --}}
@routes
@endpush

@push('title')
<title>{{ config('app.name') }} </title>
@endpush

@section('content')
<div id="app" v-cloak>
    <app />
</div>
@endsection

@push('footer_js')
@if(config('echo.realtime'))
{{-- Load socket.io --}}
<script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js" defer></script>
@endif

<script src="{{ mix('js/manifest.js') }}" defer></script>
<script src="{{ mix('js/vendor.js') }}" defer></script>
<script src="{{ mix('js/app.js') }}" defer></script>
@endpush

ProvidesScriptVariables.php

<?php

namespace App\Ezybyz\Configuration;

use App\Ezybyz\Ezybyz;
use Illuminate\Support\Facades\Auth;
use App\Ezybyz\Contracts\InitialFrontendState;

trait ProvidesScriptVariables
{
    /**
     * Get the default JavaScript variables for Spark.
     *
     * @return array
     */
    public static function scriptVariables()
    {
        return [
            'csrfToken' => csrf_token(),
            'env' => config('app.env'),
            'api_endpoint' => config('ezybyz.app.api'),
            'sponsor' => self::getSponsor(),
        ];
    }

    protected static function getState()
    {
        return Ezybyz::call(InitialFrontendState::class . '@forUser', [Auth::user()]);
    }

    protected static function getSponsor()
    {
        if ($link = request()->referrallink) {
            $user = Ezybyz::user()->find($link->user_id);

            return [
                'user_id' => $user->id,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'email' => $user->email,
                'profile' => $user->profile,
            ];
        }
        // We Will Return a Default Sponsor
        else {
            $user = Ezybyz::user()->first()->load('profile');

            return [
                'user_id' => $user->id,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'email' => $user->email,
                'profile' => $user->profile,
            ];
        }
    }
}

Any help is greatly appreciated.

Upvotes: 6

Views: 14718

Answers (5)

Ali
Ali

Reputation: 1

This answer might be late but may help new comers.

Is your XAMPP/WAMP server running check and make sure it is running.

because 500 is an internal serve error and it mainly occurs when your server is not responding or not running properly.

hope this will help you

Upvotes: 0

ako precious
ako precious

Reputation: 91

add $this->withoutExceptionHandling(); before $response = $this->get('/'); to get more information about the error.

Upvotes: 3

colorado
colorado

Reputation: 1

One possible cause of this error message is, you don't have the openssl php extension enabled.

Upvotes: 0

Abhishek Verma
Abhishek Verma

Reputation: 1

Do check with your routes. Check if you have provided 'name' to your base route.

eg: In routes/web.php, Do the following:

Route::get('home','BlogController@home')->name('home');

Now go to the test case and do replace the following code:

$response = $this->get('home');

Let me know if it works for you.

Upvotes: -1

yesnik
yesnik

Reputation: 4695

Try to use RefreshDatabase trait in the test class:

use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

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

        $response->assertStatus(200);
    }
}

Upvotes: 11

Related Questions