Pixelairport's avatar

Should I test fortify register

Hi. I want to test, if registration works correct. I use fortify. But when the test builds the routes list, it looks at the config file of fortify. If the register feature is not set, then the route does not exist. But how can I do a test where I change the route list in one test. I mean, how can I rebuild the route list, after I set a config var in test runtime? Found things like that:

Route::getRoutes()->refreshNameLookups();

But it does not work.

0 likes
1 reply
LaryAI's avatar
Level 58

To test the registration feature in Fortify, you can create a test that sends a POST request to the registration endpoint with valid data and asserts that the user was created in the database. Here's an example test:

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Tests\TestCase;

class RegistrationTest extends TestCase
{
    use RefreshDatabase;

    public function testUserCanRegister()
    {
        if (! Features::enabled(Features::registration())) {
            $this->markTestSkipped('Registration feature is not enabled.');
        }

        $response = $this->post('/register', [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertRedirect('/home');

        $this->assertDatabaseHas('users', [
            'name' => 'John Doe',
            'email' => '[email protected]',
        ]);
    }
}

Note that the test checks if the registration feature is enabled before running the test. If it's not enabled, the test is skipped.

If you need to change the configuration of Fortify in the test runtime, you can use the config() function to set the configuration values. For example, to enable the registration feature, you can add this to the test:

config(['fortify.features' => [Features::registration()]]);

This sets the fortify.features configuration value to an array that contains only the registration feature. You can add this line before the test or inside the test method, depending on your needs.

Hope this helps!

Please or to participate in this conversation.