Kristian Mandrup
2010-Jun-10 16:21 UTC
[rspec-users] RSpec 2: uninitialized constant - mocking belongs_to AR relation
gems/ruby-1.9.2-head/bundler/gems/rspec-
core-2398fcadf5beb256bed9c548c59445d3b4c8a047-master/lib/rspec/core/
backward_compatibility.rb:26:in `const_missing'': uninitialized
constant Message::User (NameError)
from /Users/kristianconsult/.rvm/gems/ruby-1.9.2-head/bundler/gems/
rspec-expectations-996c752171a0a0e16347e934dadc25767e31186c-master/lib/
rspec/expectations/backward_compatibility.rb:6:in `const_missing''
from /Users/kristianconsult/Development/Languages/Ruby/Apps/Web-apps/
Rails/Rails-3/Experimental/views_example/app/models/message.rb:2:in
`<class:Message>''
from /Users/kristianconsult/Development/Languages/Ruby/Apps/Web-apps/
Rails/Rails-3/Experimental/views_example/app/models/message.rb:1:in
`<top (required)>''
--
class Message < ActiveRecord::Base
belongs_to :recipient, :class_name => User.name
validates_presence_of :title, :text, :recipient
end
--
require ''spec_helper''
class User
end
describe Message do
before(:each) do
@message = Message.new(
:title => "foo",
:text => "bar",
:recipient => mock_model(User)
)
end
...
--
If I uncomment the :class_name option on the belongs_to relation of
Message:
Failure/Error: @message = Message.new(
uninitialized constant Message::Recipient
Hmm??
Kristian Mandrup
2010-Jun-13 11:03 UTC
[rspec-users] RSpec 2: uninitialized constant - mocking belongs_to AR relation
Cause of error: class Message < ActiveRecord::Base belongs_to :recipient #, :class_name => User.name -- it then by convention expects there to be a model class called Recipient. should instead be class Message < ActiveRecord::Base belongs_to :recipient, :class_name => User.name But then the User class must be a subclass of ActiveRecord, or have a name method that returns "User" or even more simple: class Message < ActiveRecord::Base belongs_to :recipient, :class_name => "User"
Seemingly Similar Threads
- undefined method `run_all' for :Array
- Can't get gem bundler to work with either rvm 1.9.1 and 1.9.2
- Rails 3 Problem: no such file to load -- rails (LoadError)
- CRITICAL: Rails 3 error : parser.bundle unhandled exception!!!
- `method_missing': undefined method `i18n' for #<Rails::Railtie::Configuration:0x000001010fce98>