почему возвращает 302 code когда нужно отправить email при тестировании feature test Laravel а через Postman все ок?

Рейтинг: 0Ответов: 1Опубликовано: 02.07.2023

Из стандартной laravel auth:

Route::post('/password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');


final class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;
}


trait SendsPasswordResetEmails
{
/**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $this->credentials($request)
        );

        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($request, $response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }
}


class AuthTest extends TestCase
{
/**
     * Send Reset Link Email
     * 
     * @return void
     */
    public function test_send_reset_link_email(): void 
    {
        Artisan::call('passport:install');
        $user = $this->getRegisteredUser();
        $response = $this->post('api/auth/password/email', [
                'email' => $user->email,
            ],
        );
        $response->assertSuccessful();
    }
}

При запуске ./vendor/bin/sail php artisan test выдает:

Tests\Feature\AuthTest > send reset link email
  Expected response status code [>=200, <300] but received 302.
  Failed asserting that false is true.

Ответы

▲ 0Принят
/**
     * Send Reset Link Email
     * 
     * @return void
     */
    public function test_send_reset_link_email(): void 
    {
        Artisan::call('passport:install');
        $user = $this->getRegisteredUser();
        $response = $this->post('api/auth/password/email', 
            ['email' => $user->email],
            ['Accept' => 'application/json']
        );
        $response->assertSuccessful();
    }