Friday, February 3, 2012

[Rails] ActiveRecord Associations not working in RSpec with Devise

Hi everyone,

I have the following classes:

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable

has_many :comments, :dependent => destroy
...
end

class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :commentable, :polymorphic => true
...
end

In rails console, I can successfully create a new comment via user:

> @attr = { :title => "Something", :content => "Something else" }
> @user.comments.create!(@attr) # this command runs successfully

However, when I try to do the same thing in RSpec, I get the following
error:

Failure/Error: @user.comments.create!(@attr)
NoMethodError:
undefined method `comments' for #<User:0x00000004867000>

Has anyone seen this before? Could it have anything to do with the
combination of RSpec and Devise?

Thanks in advance,

Mike

I'm using Rails 3.1.0, Ruby 1.9.2p290, and RSpec 2.6.4.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate