LoginSignup
4
2

More than 3 years have passed since last update.

Rails6系×PostgreSQLでRSpecの実行環境を作成する

Last updated at Posted at 2020-08-25

RSpecが使用出来るように、railsアプリケーションを新規で作成し、gem rails-rspecを導入したときの備忘録です。

実行環境

  • Rails 6.0.3.2
  • postgres (PostgreSQL) 12.4
  • rspec-rails 4.0.1

手順

① rails newでrailsアプリケーションを新規作成

$ rails new アプリケーション名 -d データベース名 -T
  • -dオプションで、データベースの種類を指定出来ます。デフォルトはsqlite3です。今回はpostgresqlを使用したため-d postgresqlと指定。
  • -Tオプションで、test::unitを組み込まないようにスキップする事が出来ます。今回はRSpecの実行環境を作成したかったので、railsのフレームワークでデフォルトで提供しているminitestの設定はスキップします。

②今回作成したアプリケーションのディレクトリに移動

$ cd アプリケーション名

③ DBを作成

$ rails db:create

④ Gemfileの以下のグループに最新の安定版のrspec-railsを追加し、gemをインストール

Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 4.0.1'
end
$ bundle

⑤generateコマンドでRSpecに必要なディレクトリや設定ファイルを作成

$ rails g rspec:install

以上の手順で簡単にRSpecの実行環境が作成出来ます!

注意事項

手順③で以下のエラーメッセージが出るかと思いますが、これはpostgresが起動していないためですので、起動させるコマンドを実行すれば解決します!

エラーメッセージ
connections on Unix domain socket “/tmp/.s.PGSQL.5432”?

postgresの起動コマンド

$ postgres -D /usr/local/var/postgres

postgresコマンドがnot foundとなる場合は、brewでpostgresを入れられていない事が考えられるので、その場合はbrew install postgresqlでインストールしてください。brewにpostgresが入っているかは以下のコマンドで確認する事が出来ます。

brewの中身を一覧で確認するコマンド

$ brew list

また、上記のpostgresの起動コマンドが動かない場合、postgresのコマンド実行ファイルが存在するかを確認してみると良いです。

コマンド実行ファイルの格納場所(今回の場合postgres)を確認するコマンド

$ which postgres

自分の場合はpostgresが以下のパスに存在していて、問題なく動きました!
/usr/local/bin/postgres

参考文献

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2