[Feature Request] Do not set ENV['RAILS_ENV'] = 'test'

12 views
Skip to first unread message

Kiril Mitov

unread,
Dec 5, 2020, 2:26:02 PM12/5/20
to rspec
Hi, 

I would like to propose a feature where the RAILS_ENV is set to test only when it is not set previously. Something like

```
  task :prepare do
    ENV['RACK_ENV'] = ENV['RAILS_ENV'] ||= 'test'
    if Rails.configuration.generators.options[:rails][:orm] == :active_record
      if Rake::Task.task_defined?("test:prepare")
        Rake::Task["test:prepare"].invoke
      end
    end
  end
```

Here is the reason.

I would like to set up an rspec system spec that tests the integration between Uppy, Aws, Shrine and my app. 

There will be one or two such spec, but I think they could grow to about 5-6 for testing with the payment service, youtube and some other services to which I would like to make real requests. 

It feels natural for me to create a new environment called 'integration_test', where we would test the actual request and response. Again, from the thousands of specs that we would be running these are some 5-10 specs. In a test env this services are mocked. In the 'integration_test' these service are not mocked. In the AWS, Shrine, Uppy case there are log of things (shrine plugins) that are initialized with config/initializers/shine.rb like the plugins. This makes it not possible to change the specific configuration for the spec as the before :each is loaded after the initializers.

I would like to suggest the change the code "ENV['RACK_ENV'] = ENV['RAILS_ENV'] ||= 'test'". 

I was looking at the rspec rails code and the RAILS_ENV seems to be set but never used after that so there is no real need for setting it. 

What do you think?

Looking forward to hearing from you.

Phil Pirozhkov

unread,
Dec 5, 2020, 2:45:01 PM12/5/20
to Kiril Mitov
RAILS_ENV seems to be set but never used after

It's because it's for Rails to use, not RSpec

What you plan is quite an untypical approach to the‎ problem.

Since you stub those external services in 'test'‎ somewhere, I suggest you stub them selectively, for all spec folders except those with your external service integration tests.

Introducing yet another env comes at a cost, just think about yet another DB.

Jon Rowe

unread,
Dec 6, 2020, 5:36:36 AM12/6/20
to rs...@googlegroups.com
You can override `spec:prepare` by defining your own task with the same name in your Rakefile, rspec-rails’s one is there to conform to the Rails convention.

thebravoman

unread,
Dec 8, 2020, 5:31:58 AM12/8/20
to rs...@googlegroups.com
Thanks.

Yes, I would like to run rspec in a different env than test. I would like to have two env environments. The first with memory services that are either mocked or as dummy services and the other with real services responding to real requests. 
We spend a lot of time debugging a problem that we occurring completely outside of our code and control - it is between AWS, Shrin, Uppy and I would like to automate the process of developing and running specs that connect with real services. For this I need a new environment. 

@Phil I think there is a use case for such an rspec
@Joh i think I could override it. 

Is there an actual reason why the env should be set to "test". Something that rspec depends on? I understand that it should have a default value that is probably "test" but why is it not possible to run rspec in a different env?

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/naE04EDa3gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/dejalu-217-524d35fb-0a92-46d0-adb8-ab08509601d6%40jonrowe.co.uk.


--
Axlessoft
Developing solutions for engineers. Bringing 3D on the web. 

Phil Pirozhkov

unread,
Dec 8, 2020, 6:26:22 AM12/8/20
to Jack Royal-Gordon
Can you please clarify, is there something in rspec-rails that
prevents you from having a separate env?
There's this
https://github.com/rspec/rspec-rails/blob/13a8a01cc07c93db97b5f1cd57229527441cd626/lib/generators/rspec/install/templates/spec/rails_helper.rb#L3

ENV['RAILS_ENV'] ||= 'test'

With `||=` you can override `RAILS_ENV` when running your tests in a
different environment with `RAILS_ENV=integration rspec`.
Or you can create an additional `spec/integration_helper.rb` that would have

ENV['RAILS_ENV'] ||= 'integration'

instead, and `require 'integration_helper'` from your specs that are
intended to be run in that env.

Just one word of precaution, I recommend separating those other specs
somehow to reduce the risk of a situation when calling `rspec` would
BOTH load specs that require `rails_helper` and `integration_helper`.

Please feel free to share your experience with this setup.

- Phil

thebravoman

unread,
Dec 8, 2020, 7:21:24 AM12/8/20
to rs...@googlegroups.com
Yes, What prevents me is the following code.

```
  task :prepare do
    ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
    if Rails.configuration.generators.options[:rails][:orm] == :active_record
      if Rake::Task.task_defined?("test:prepare")
        Rake::Task["test:prepare"].invoke
      end
    end
  end
```

ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
```
Which means that it is always setting the env to be 'test' 

I would like to change this code to 
```
ENV['RACK_ENV'] = ENV['RAILS_ENV'] ||= 'test'
```

--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/naE04EDa3gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.

Phil Pirozhkov

unread,
Dec 8, 2020, 8:06:54 AM12/8/20
to Jack Royal-Gordon
It's a rake tast, you can override it.
I'd rather not change it to `||=` to avoid the development DB from
being wiped out for those who explicitly set their `RAILS_ENV` to
`development`.

Please share your experience with how your setup works.

Even though your case is a legitimate use, still there are other
legitimate usages that might become completely broken because of such
a change.
Thanks for understanding.

- Phil

thebravoman

unread,
Dec 8, 2020, 8:45:41 AM12/8/20
to rs...@googlegroups.com
Aha.
Ok. That makes a lot of sense. I could override the rake task. 

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/naE04EDa3gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages