25
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpecでRails.envをstubし、test環境以外の振る舞いのspecを書く

Last updated at Posted at 2014-06-30

答え

たとえば、本番環境の振る舞いをテストしたいときは下記のようにする。

RSpec 3

expect(Rails).to receive(:env).and_return('production'.inquiry)

RSpec 2

Rails.stub(:env).and_return('production'.inquiry)

解説

下記のようにすると、 Rails.envActiveSupport::StringInquirer のインスタンスだということがわかります。

[1] pry(main)> Rails.env.class
=> ActiveSupport::StringInquirer

ソース見るのが早いと思いますが、 ActiveSupport::StringInquirer は文字列が等しいかどうかをメソッドで聞けるようになります。また、 String#inquiry追加されるので、下記のような振る舞いになります。

[2] pry(main)> 'production'.inquiry
=> "production"
[3] pry(main)> 'production'.inquiry.class
=> ActiveSupport::StringInquirer
[4] pry(main)> 'production'.inquiry.production?
=> true
25
23
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
25
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?