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


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


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

 Failure/Error: @user.comments.create!(@attr)
   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,


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs