method not found with Rails 3.1.0RC1 #171

Closed
davidsf opened this Issue May 22, 2011 · 9 comments

7 participants

@davidsf

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

@AndreasWurm

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"
end
require 'spec_helper'

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

I'm using test/unit without rspec.

group :test do
  gem 'shoulda'
end

test_helper:

require 'shoulda'

test/unit/stop_test.rb

require 'test_helper'

class StopTest < ActiveSupport::TestCase
  should belong_to :trip
end
#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>'
@rmm5t

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'
@davidsf

Thanks Ryan, with the workaround works great.

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

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.

@lucassus

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

@weeksie

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.

@gtd

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?

@gabebw
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