You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I then create a service which uses this constant app/services/test_service.rb
class TestService
include Constants
def do
NAME
end
end
I then write a spec for this service spec/services/test_service_spec.rb
require 'rails_helper'
RSpec.describe TestService do
it 'can be run' do
expect(TestService.new.do).to eq('hi')
end
end
If I run rspec at this time, this fails as expected because I am missing a require directive for lib/constants.rb: uninitialized constant TestService::Constants (NameError)
I then add a model (Personapp/models/person.rb) and require the constants file.
require 'constants'
class Person < ActiveRecord::Base
end
I then write a spec for Personspec/models/person_spec.rb
require 'rails_helper'
RSpec.describe Person, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Running rspec passes (false positive). Running rspec spec/services does not pass (expected behavior).
It appears that RSpec is loading the file lib/constants.rb into memory when it runs person_spec.rb but not releasing that reference when it goes to run the next spec. This is an issue because my services fail in production when I do not preload the entire rails app.
As surprising as this is, it is expected behavior. RSpec currently does not run each spec in a new process or thread. It runs all specs in a single process/thread. Ruby semantics are load files on require. It doesn't unload anything. This is the same behavior you'd see if your ran your Rails app.
I believe you are looking for the feature under discussion here: rspec/rspec-core#1254
If this is seriously something you would like to test, you can look at how we use in_sub_process in rspec-core to isolate things like this, and then wrap all of your tests in that, but it's beyond the scope of rspec to do this for you.
I define a constants file at
lib/constants.rb
I then create a service which uses this constant
app/services/test_service.rb
I then write a spec for this service
spec/services/test_service_spec.rb
If I run
rspec
at this time, this fails as expected because I am missing arequire
directive forlib/constants.rb
:uninitialized constant TestService::Constants (NameError)
I then add a model (
Person
app/models/person.rb
) and require theconstants
file.I then write a spec for
Person
spec/models/person_spec.rb
Running
rspec
passes (false positive). Runningrspec spec/services
does not pass (expected behavior).It appears that RSpec is loading the file
lib/constants.rb
into memory when it runsperson_spec.rb
but not releasing that reference when it goes to run the next spec. This is an issue because my services fail in production when I do not preload the entire rails app.Rails: 4.2.2
RSpec-rails: 3.3.3
Sample project here: https://github.com/workmaster2n/rspec_environment_issue
The text was updated successfully, but these errors were encountered: