File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
1212
13- _ 1335 TILs and counting..._
13+ _ 1336 TILs and counting..._
1414
1515---
1616
@@ -848,6 +848,7 @@ _1335 TILs and counting..._
848848- [ Mask An ActiveRecord Attribute] ( rails/mask-an-activerecord-attribute.md )
849849- [ Merge A Scope Into An ActiveRecord Query] ( rails/merge-a-scope-into-an-activerecord-query.md )
850850- [ Migrating Up Down Up] ( rails/migrating-up-down-up.md )
851+ - [ Mock Rails Environment With An Inquiry Instance] ( rails/mock-rails-environment-with-an-inquiry-instance.md )
851852- [ Order Matters For ` rescue_from ` Blocks] ( rails/order-matters-for-rescue-from-blocks.md )
852853- [ Params Includes Submission Button Info] ( rails/params-includes-submission-button-info.md )
853854- [ Params Is A Hash With Indifferent Access] ( rails/params-is-a-hash-with-indifferent-access.md )
Original file line number Diff line number Diff line change 1+ # Mock Rails Environment With An Inquiry Instance
2+
3+ As discussed in [ Make A String Attribute Easy to Inquire
4+ About] ( make-a-string-attribute-easy-to-inquire-about.md ) , the ` Rails.env ` is
5+ assigned an instance of ` ActiveSupport::StringInquirer ` . This allows us to ask
6+ whether the current Rails environment is ` #production? ` , ` #development? ` , etc.
7+
8+ With this in mind, we can have a test execute in a specific environment by
9+ mocking how ` Rails.env ` responds. Though the actual env for a test is going to
10+ be ` test ` , we can simulate a different environment with an RSpec ` before ` block
11+ like the following:
12+
13+ ``` ruby
14+ before do
15+ allow(Rails ).to receive(:env ) { " staging" .inquiry }
16+ end
17+ ```
18+
19+ Or similarly, to simulate the ` production ` environment:
20+
21+ ``` ruby
22+ before do
23+ allow(Rails ).to receive(:env ) { " production" .inquiry }
24+ end
25+ ```
26+
27+ The ` #inquiry ` being monkey-patched onto the ` String ` class gives you the
28+ willies, you could do the following instead:
29+
30+ ``` ruby
31+ before do
32+ allow(Rails ).to receive(:env ) do
33+ ActiveSupport ::StringInquirer .new (" production" )
34+ end
35+ end
36+ ```
37+
38+ [ source] ( https://stackoverflow.com/a/25134591/535590 )
You can’t perform that action at this time.
0 commit comments