mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Mock Rails Environment With An Inquiry Instance as a Rails TIL
This commit is contained in:
38
rails/mock-rails-environment-with-an-inquiry-instance.md
Normal file
38
rails/mock-rails-environment-with-an-inquiry-instance.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Mock Rails Environment With An Inquiry Instance
|
||||
|
||||
As discussed in [Make A String Attribute Easy to Inquire
|
||||
About](make-a-string-attribute-easy-to-inquire-about.md), the `Rails.env` is
|
||||
assigned an instance of `ActiveSupport::StringInquirer`. This allows us to ask
|
||||
whether the current Rails environment is `#production?`, `#development?`, etc.
|
||||
|
||||
With this in mind, we can have a test execute in a specific environment by
|
||||
mocking how `Rails.env` responds. Though the actual env for a test is going to
|
||||
be `test`, we can simulate a different environment with an RSpec `before` block
|
||||
like the following:
|
||||
|
||||
```ruby
|
||||
before do
|
||||
allow(Rails).to receive(:env) { "staging".inquiry }
|
||||
end
|
||||
```
|
||||
|
||||
Or similarly, to simulate the `production` environment:
|
||||
|
||||
```ruby
|
||||
before do
|
||||
allow(Rails).to receive(:env) { "production".inquiry }
|
||||
end
|
||||
```
|
||||
|
||||
The `#inquiry` being monkey-patched onto the `String` class gives you the
|
||||
willies, you could do the following instead:
|
||||
|
||||
```ruby
|
||||
before do
|
||||
allow(Rails).to receive(:env) do
|
||||
ActiveSupport::StringInquirer.new("production")
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
[source](https://stackoverflow.com/a/25134591/535590)
|
||||
Reference in New Issue
Block a user