2011-12-11 3 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 

はまだ

を通過するすべてのテストになり破壊せずに、Userモデル

私のテスト

マイモデルから破壊します

答えて

0

は....あなたのコードの

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

一つのライン#1、あなたのテストのロジックに誤りがあるように見える、私はあなたがそれを破壊呼び出すときにゼロとなり、ライン#2に@followed考えます@ user.followersには "nil"オブジェクトはありません。そのため、おそらくあなたのテストは引き続き成功しています。私は次のコードを使用し、期待通りに機能しました:

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 
関連する問題