Here is the alternative way from rails test doc https://guides.rubyonrails.org/testing.html
post articles_url, params: { article: { body: "Rails is awesome!", title: "Hello Rails" } }, headers: { Authorization: ActionController::HttpAuthentication::Basic.encode_credentials(@user.email_address, "password") }
Thank you.