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

Uninitialized constant ActiveSupport::TestCase #763

Closed
ybur-yug opened this issue Jul 24, 2015 · 12 comments
Closed

Uninitialized constant ActiveSupport::TestCase #763

ybur-yug opened this issue Jul 24, 2015 · 12 comments

Comments

@ybur-yug
Copy link

When configuring shoulda-matchers in a small project I have, I have been unable to get things running at all. I am pointing to 3.0.0.rc1.

This is my configuration:

...
require 'shoulda/matchers'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
...

It is a typical rails project, and this is included in spec/rails_helper.rb.

When guard runs (or if I run manually its identical) my specs I get:

...
[gems path]/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/integrations/libraries
/rails.rb:19:in `integrate_with': uninitialized constant ActiveSupport::TestCase (NameError)

(broke the lines and shortened path for readability)

I know its a release candidate but couldn't find another issue. For reference, I am on rails 4.2.3 and rspec 3.3.2. I can gladly provide any other information you would want or need.

@mcmire
Copy link
Collaborator

mcmire commented Jul 24, 2015

This seems odd to me... is there anything special about your setup? It shouldn't be looking for ActiveSupport::TestCase, but it shouldn't be failing to find it, either...

@ybur-yug
Copy link
Author

This is a pretty boilerplate rails app. Was making it for a tutorial I'm doing at a beginners meetup. I since deleted the project and just required 2.8 in a new one and it worked fine.

Might not be worth keeping open? I can try to replicate and send you a repository if you think it is actionable, though.

😖

@ybur-yug
Copy link
Author

@mcmire I have done some research on this and put together a sample repository. So this is the exact setup I have in each situation:
rspec 3.3.0

rspec-rails 3.3.3

shoulda-matchers 3.3.0.rc1

Specific to these tests. The tests can be found in spec/models/list_spec.rb and spec/models/task_spec.rb. They are very simple:

# list_spec.rb
describe List do
  it { is_expected.to validate_presence_of :name }
  it { is_expected.to have_many :tasks }
end

# task_spec.rb
describe Task do
  it { is_expected.to validate_presence_of :description }
  it { is_expected.to belong_to :list }
end

In this repository I have 2 commits. One with shoulda 3.3.0.rc1 and one with 2.8.0 where it is working.

Commit With Error

Here the specific, full trace is:

bobby@devbox:~/code/rails_refresh/todos$ rspec spec/models/
[rvm path]/shoulda/matchers/integrations/libraries/rails.rb:19:in `integrate_with': uninitialized constant ActiveSupport::TestCase (NameError)
    [rvm path]/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/integrations/configuration.rb:48:in `block (2 levels) in apply'
    [rvm path]/ruby-2.2.2/lib/ruby/2.2.0/set.rb:283:in `each_key'
    [rvm path]/ruby-2.2.2/lib/ruby/2.2.0/set.rb:283:in `each'
    [rvm path]/ruby-2.2.2/bundler/gems/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/integrations/configuration.rb:48:in `block in apply'
    [rvm path]/ruby-2.2.2/lib/ruby/2.2.0/set.rb:283:in `each_key'
    [rvm path]/ruby-2.2.2/lib/ruby/2.2.0/set.rb:283:in `each'
    [rvm path]/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/integrations/configuration.rb:46:in `apply'
    [rvm path]/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/integrations/configuration.rb:9:in `apply'
    [rvm path]/shoulda-matchers-a3fb06c369c2/lib/shoulda/matchers/configuration.rb:16:in `integrate'

Commit With Working Current Version of Gem

and running specs goes fine:

bobby@devbox:~/code/rails_refresh/todos$ rspec spec/models
....

Finished in 0.84229 seconds (files took 1.38 seconds to load)
4 examples, 0 failures

The source is is pointing to in lib/shoulda/matchers/integrations/libraries/rails.rb is:

...
          def integrate_with(test_framework)
            Shoulda::Matchers.assertion_exception_class =
              ActiveSupport::TestCase::Assertion

            SUB_LIBRARIES.each do |name|
              library = Integrations.find_library!(name)
              library.integrate_with(test_framework)
            end
          end
...

Let me know if there is anything I can do.

@mcmire
Copy link
Collaborator

mcmire commented Jul 25, 2015

You shouldn't be requiring shoulda-matchers in your spec_helper. The gem relies on Rails to have been loaded. That hasn't happened yet when spec_helper is loaded. You need to require and configure the gem in rails_helper instead.

@ybur-yug
Copy link
Author

Ah, got it. Thanks. 👍

@betesh
Copy link
Contributor

betesh commented Oct 7, 2015

I'm using shoulda-matchers outside of Rails, in a standalone ActiveRecord environment. I need to configure the test environment before loading my application code. Here's what worked for me:

require "shoulda-matchers"
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec

   # This require statement solves the uninitialized constant / NameError issue
    require "active_record" 
    with.library :active_record
    with.library :active_model
  end
end

@mcmire
Copy link
Collaborator

mcmire commented Oct 7, 2015

Yup! shoulda-matchers makes the assumption you've already required ActiveRecord. It doesn't make that require on its own anymore.

That would be a good error message to have.

@serixscorpio
Copy link

Thanks @betesh! I'm using shoulda-matchers outside of rails as well, using only ActiveModel. Based on your feedback, I have this which worked:

require "shoulda/matchers"
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
   # This require statement solves the uninitialized constant / NameError issue
    require "active_model" 
    with.library :active_model
  end
end

@mcmire Is this the recommended way?

@mcmire
Copy link
Collaborator

mcmire commented Oct 29, 2015

@serixscorpio Yup, that would work. You could put the require at the top so it doesn't get lost, but that's just a suggestion. (Also, FYI, you don't need to explicitly require shoulda-matchers anymore.)

@dchersey
Copy link

I'm running with shoulda-matchers 3.1.1 and also getting this reference.
My rails_helper has

require 'spec_helper'
require 'shoulda/matchers'
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    with.library :rails
  end
end

I am getting

shoulda-matchers-3.1.1/lib/shoulda/matchers/integrations/libraries/rails.rb:20:in `integrate_with': uninitialized constant ActiveSupport::TestCase (NameError)
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/integrations/configuration.rb:48:in `block (2 levels) in apply'
    from /Users/david/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/set.rb:283:in `each_key'
    from /Users/david/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/set.rb:283:in `each'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/integrations/configuration.rb:48:in `block in apply'
    from /Users/david/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/set.rb:283:in `each_key'
    from /Users/david/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/set.rb:283:in `each'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/integrations/configuration.rb:46:in `apply'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/integrations/configuration.rb:9:in `apply'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/configuration.rb:16:in `integrate'
    from /Users/david/projects/feelwe/spec/rails_helper.rb:14:in `block in <top (required)>'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/configuration.rb:5:in `configure'
    from /Users/david/projects/feelwe/spec/rails_helper.rb:13:in `<top (required)>'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `require'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `block in requires='
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `each'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `requires='
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `each'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `process_options_into'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:21:in `configure'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:105:in `setup'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:92:in `run'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:78:in `run'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:45:in `invoke'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/gems/rspec-core-3.4.2/exe/rspec:4:in `<top (required)>'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/bin/rspec:23:in `load'
    from /Users/david/projects/feelwe/vendor/ruby/2.2.0/bin/rspec:23:in `<main>'

which comes from code:

          def integrate_with(test_framework)
            Shoulda::Matchers.assertion_exception_class =
              ActiveSupport::TestCase::Assertion

            SUB_LIBRARIES.each do |name|
              library = Integrations.find_library!(name)
              library.integrate_with(test_framework)
            end
          end

I'm running Rails 4.2.5 and apparently I don't have this part of active support loaded. Not sure why, except that I generated the app using rspec and not test:unit.

Any idea on what I can do to resolve this dependency, or why I need it in the first place?

My gemfile has

 gem 'minitest'              
 gem 'shoulda-matchers', '~> 3.1'

Thanks!

@variousred
Copy link

@dchersey I'm getting the exact same thing.
Side note: I noticed you have shoulda configured for rspec, but your gemfile has minitest.

@dchersey
Copy link

I went back to 3.1.1 and added require: false to my gemfile and it resolved
my original issue of the matchers not being picked up. I am not sure what
is up with this latest release; it seems shoulda does not support it
(perhaps for this reason).

My gemfile now has:

gem "shoulda", require: false
gem 'shoulda-matchers', require: false

This gives me

  • shoulda (2.11.3)
  • shoulda-kept-assign-to (1.1.0)
  • shoulda-matchers (3.1.1)
    Hope this helps!
    On Mon, Feb 22, 2016 at 1:43 PM Michael Mitchell notifications@github.com
    wrote:

@dchersey https://github.com/dchersey I'm getting the exact same thing.


Reply to this email directly or view it on GitHub
#763 (comment)
.

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

6 participants