skip to Main Content

I’m trying to use the Pest PHP framework for testing, but I’m unable to perform a simple test.

it('can view home page', function () {
    $response = $this->get('/');

    expect($response->status())->toBe(200);
});

I always get the error:

Failed asserting that 404 is identical to 200.

It seems that the URL is not being correctly read. Here’s my files configuration:

.env file

APP_NAME="My software name"
APP_ENV=local
APP_KEY=base64:abchdX+Hm/LYphNZIBx942SFZoJYzGUFGidCxQifqMw=
APP_DEBUG=true
APP_URL=http://localhost/mysoftware

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </source>
    <php>
        <env name="APP_ENV" value="local"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <!-- <env name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <env name="DB_DATABASE" value=":memory:"/> -->
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

I’ve read some topics such as:

https://www.reddit.com/r/laravel/comments/kivwyr/laravel_phpunit_test_pointing_to_wrong_url/

Configuring Laravel 5 Unit Test URL with phpunit.xml

Laravel phpunit not getting right url

Where I end up trying to rectify my phpunit.xml file by appending:

<env name="APP_URL" value="http://localhost/mysoftware"/>

But the error persists. What can I do to debug further?


Edit 1) – My routes files indeed have both / & /login routes and it doesn’t matter if the test is pointing to home route or login route, the error is the same.

it('can view home page', function () {
    $response = $this->get('/');

    expect($response->status())->toBe(200);
});

it('can view login page', function () {
    $response = $this->get('/login');

    expect($response->status())->toBe(200);
});

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Solved.

    Dunno why, but got it working by changing the .env file from this:

    APP_URL=http://localhost/mysoftware
    

    to this

    APP_URL=http://localhost
    

    No need to change the phpunit.xml file (at least in my case...)


  2. Try to remove the config cache using this command :

    php artisan config:cache
    

    And try it with this config in your phpunit.xml:

    <server name="APP_URL" value="http://localhost"/>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search