2011-12-11 34 views
0

對於第12章練習1,我很難弄清楚爲什麼我的測試不會失敗出:依賴=>:從用戶模型Rails教程Ch 12練習1 - 取出後無法測試失敗:dependent =>:destroy

我測試

我的模型破壞,而不:依賴=>:破壞

has_many :relationships, 
    :foreign_key => "follower_id" 
has_many :following, :through => :relationships, :source => :followed 
has_many :reverse_relationships, 
    :foreign_key => "followed_id" 
    :class_name => "Relationship" 
has_many :followers, :through => :reverse_relationships, :source => :follower 

仍導致傳遞

的所有測試

回答

0

有似乎是在您的測試邏輯錯誤....

#1 @followed.destroy 
    #2 @user.followers.should_not include(@followed) 

一行代碼的#1,我覺得@followed變爲零,當你調用摧毀它,並在線路#2 ,@ user.followers中沒有「無」對象,所以這可能就是爲什麼你的測試繼續傳遞。我用下面的代碼,它按預期工作:

r1 = @user.follow!(@followed) 
    r2 = @followed.follow!(@user) 
    @user.destroy 
    [r1, r2].each do |relationship| 
    Relationship.find_by_followed_id(relationship.followed_id).should be_nil 
    end 
相關問題