ROR rspec-rails error: ActiveRecord: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied

ROR rspec-rails error: ActiveRecord: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied

我正在做 RoR 请求测试,我收到了这个错误:

An error occurred while loading ./spec/requests/posts_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::ConnectionNotEstablished:
  connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied
# ./spec/rails_helper.rb:28:in `<top (required)>'
# ./spec/requests/posts_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::ConnectionBad:
#   connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied
#   ./spec/rails_helper.rb:28:in `<top (required)>'

这是我的测试代码:

require 'rails_helper'
RSpec.describe 'Users', type: :request do
  describe 'GET #index' do
    before(:example) { get "/users" }

    it 'is a success' do
      expect(response).to have_http_status(:ok)
    end

    it "renders 'index' template" do
      expect(response).to render_template(:index)
    end

    it "shows the rendered text 'Welcome to the Ruby on Rails Blog'" do
      expect(response).to include('Welcome to the Ruby on Rails Blog')
    end
  end
end

我在 database.yml 中提供了我的用户名和密码,但我不知道为什么会出现此错误。 我的 database.yml

配置
development:
  <<: *default
  database: blog_app
  host: 127.0.0.1
  username: {username}
  password: {password}

您指定了开发数据库。在不同的测试下 运行。

将以下内容添加到您的database.yml

test:
  <<: *default
  database: test_blog_app
  host: 127.0.0.1
  username: {username}
  password: {password}