method not found with Rails 3.1.0RC1 #171

davidsf opened this Issue May 22, 2011 · 9 comments

7 participants


I have updated an app to Rails 3.1.0RC1 and the tests now give the error:

undefined method `have_many' for UserTest:Class (NoMethodError)

with all the shoulda matchers. Is like doesn't load shoulda but I have require 'shoulda' in test_helper.rb and require 'test_helper.rb' in the test file (the same test works with Rails 3.0.x).

Rails 3.1.0.rc1 with shoulda 2.11.3


I also use rails 3.1 rc1 and I experience no problems

group :test do
  gem "rspec-rails", "~> 2.6.1.beta1"
  gem 'shoulda', "~> 2.11.3"
require 'spec_helper'

describe User do
  it { should have_many(:folders) }

I'm using test/unit without rspec.

group :test do
  gem 'shoulda'


require 'shoulda'


require 'test_helper'

class StopTest < ActiveSupport::TestCase
  should belong_to :trip
#ruby -Itest test/unit/stop_test.rb 
test/unit/stop_test.rb:4:in `<class:StopTest>': undefined method `belong_to' for StopTest:Class (NoMethodError)
    from test/unit/stop_test.rb:3:in `<main>'

Hi David, I think this is caused because the current stable version of shoulda looks for RAILS_ROOT to determine what to automatically load, but it's no longer available in Rails 3.1.

As a temporary workaround (and before Shoulda 3.0 is stable), add this to your test_helper.rb:

# Shoulda looks for RAILS_ROOT before loading shoulda/rails, and Rails 3.1
# doesn't have that anymore.
require 'shoulda/rails'

Thanks Ryan, with the workaround works great.

@davidsf davidsf closed this May 23, 2011
@rmm5t rmm5t reopened this May 23, 2011

I'm going to leave this open, just to make sure we have everything resolved in Shoulda 3.0 beta. I think it might already be handled, but I don't want to lose track of it.


For me
require 'shoulda/matchers'
fixed this problem, see: lucassus/cse@a78cf75#L3R7


This still isn't working for me. I get an undefined method validates_presence_of with Rails 3.1 and shoulda 2.11.3, even when explicitly requiring 'shoulda/rails' I'm using Test::Unit.


The require 'shoulda/rails' hack works for 2.11.3, but that is full of deprecation warnings as described #173. Updating to 3.0.0.beta renders the require 'shoulda/rails' hack ineffective.

Rails 3.1 has been out for almost 2 months now, can we expect a definitive fix soon? Or is test/unit deprecated permanently?

thoughtbot, inc. member

Hi all, does using shoulda-context 3.0.0 work? If not, please open an issue on shoulda-context.

@gabebw gabebw closed this Mar 9, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment