Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSpec Pollutes Environment #1446

Closed
workmaster2n opened this issue Aug 12, 2015 · 2 comments
Closed

RSpec Pollutes Environment #1446

workmaster2n opened this issue Aug 12, 2015 · 2 comments

Comments

@workmaster2n
Copy link

I define a constants file at lib/constants.rb

module Constants
  NAME='hi'
end

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 (Person app/models/person.rb) and require the constants file.

require 'constants'

class Person < ActiveRecord::Base
end

I then write a spec for Person spec/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.

Rails: 4.2.2
RSpec-rails: 3.3.3

Sample project here: https://github.com/workmaster2n/rspec_environment_issue

bundle
bundle exec rake db:setup
bundle exec rspec spec/services
bundle exec rspec spec/models/ spec/services/
@cupakromer
Copy link
Member

Thanks for all the detailed information 💙

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

@JonRowe
Copy link
Member

JonRowe commented Aug 13, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants