2012-05-23 73 views
1

我已經設置了項目,任務和子任務,所有這些都是值得評論的。我創建了評論模型並連接了模型,但不知道如何正確驗證用戶標識以及如何使用Rpsec和Factory Girl測試模型代碼。我也在使用Devise。引用用戶的多態關聯

評論遷移

1 class CreateComments < ActiveRecord::Migration 
    2 def change    
    3  create_table :comments do |t| 
    4  t.references :user, :null => false 
    5  t.text :comment, :null => false, 
    6       :limit => 500     
    7  t.references :commentable, :polymorphic => true 
    8   # upper line is a shortcut for this 
    9   # t.integer :commentable_id  
10   # t.string :commentable_type  
11  t.timestamps   
12  end      
13  add_index :comments, :user_id 
14  add_index :comments, :commentable_id 
15 end 
16 end 

評論模型

1 class Comment < ActiveRecord::Base 
    2   
    3 # RELATIONSHIPS 
    4 belongs_to :commentable, :polymorphic => true 
    5 belongs_to :user 
    6 # VALIDATIONS    
    7 validates :user, :presence => true 
     validates :commentable, :presence => true 
    8 validates :comment, :presence => true, 
    9      :length => { :maximum => 500 } 
10 
11 # ATTRIBUTE ASSIGNMENT 
12 attr_accessible :comment 
13 
14 end 

用戶模型

1 class User < ActiveRecord::Base 
    2   
    3 # RELATIONSHIPS 
    4 has_many :synapses, :dependent => :destroy 
    5 has_many :projects, :through => :synapses 
    6 
    7 has_many :vesicles, :dependent => :destroy 
    8 has_many :tasks, :through => :vesicles 
    9 
10 has_many :subvesicles, :dependent => :destroy 
11 has_many :subtasks, :through => :subvesicles, :dependent => :nullify 
12 
13 has_many :comments, :dependent => :destroy 
14 
15 # VALIDATIONS 
16 validates :name, :presence => true, 
17      :uniqueness => true    
18 
19 # ATTRIBUTE ASSIGNMENT 
20 attr_accessible :name, :email, :password, :password_confirmation, :remember_me 
21 
22 # DEVISE MODULES   
23 # Include default devise modules. Others available are: 
24 # :token_authenticatable, :encryptable, :lockable, :timeoutable and :omniauthable 
25 devise :database_authenticatable, :registerable, 
26   :recoverable, :rememberable, :trackable, :validatable, 
27   :confirmable  
28 
29 end 

種評論廠

105 factory :comment do 
106  comment "This is some generic comment with some generic content" 
107 end 

評論模型規格

1 require 'spec_helper'  
    2   
    3 describe Comment do 
    4 
    5 it 'Should create new comment' do 
    6  FactoryGirl.build(:comment).should be_valid 
    7 end      
    8 
    9 it 'Should respond to method provided by polymorphism to find its parent' do 
10  FactoryGirl.build(:comment).should respond_to(:commentable) 
11 end      
12 
13 end 

此第一測試目前失敗,錯誤消息說未定義的方法`用戶」爲#註釋:0xa88c354>。但是,如果我通過用戶ID像這樣...

FactoryGirl.build(:comment, :user => confirmed_user).should be_valid 

比我應該有用戶ID設置爲質量分配屬性,我不希望出現這種情況(圖有些用戶可能會干擾它的屬性和更改)。 如何正確測試和驗證它?此外,這是我第一次做多態,所以如果你看到任何愚蠢的東西,讓我知道。

編輯。作爲一個答案,我現在已經這樣做了。不幸的是,它返回相同的錯誤。

5 it 'Should create new comment' do 
    6  confirmed_user = FactoryGirl.build(:confirmed_user) 
    7  FactoryGirl.build(:comment, :commentable => confirmed_user).should be_valid 
    8 end 

回答

1

嘗試更換:

:user => confirmed_user 

有:

:commentable => confirmed_user 

UPD:

validates :user, :presence => true 

也必須是:

validates :commentable, :presence => true 
+0

我試圖和它返回相同的錯誤消息。我用我輸入的代碼更新了問題。 – oFca

+1

查看我的UPD部分。 – jdoe

+0

好的,這通過了測試!但我不明白這個模型代碼如何驗證user_id是否存在? – oFca

1

模型評論應該有:

belongs_to :user