0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ENV['RAILS_ENV']

Last updated at Posted at 2025-01-19
rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'

rspec内で実行すると

   it "サンプル" do
      p ENV['RAILS_ENV']
    end
"test"
...
Finished in 0.10224 seconds (files took 1.94 seconds to load)
4 examples, 0 failures

コンソールの環境はdevelopmentになる

************ % rails c --sandbox                                           
Loading development environment in sandbox (Rails 7.2.0)

*******(dev)> ENV['RAILS_ENV']
=> "development"

ENVオブジェクト

環境変数を表すオブジェクト。Hash と同様のインターフェースを持ちます。ただし、Hash と異なり、ENV のキーと値には文字列しかとることができません。

また、ENV で得られる文字列は Object#freeze されています。

irb(main):002> p ENV['TERM'].frozen?
true
=> true

感想

本当にRSpecはテスト環境なのか知りたかった。
実行環境がテスト環境であることを見ることができてよかった。
安心してtest用のデータベースを作成できる
どこでかは知らないが、ENV['TERM']はnilかfalseになるらしい

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?