对于第12章练习1,我无法弄清楚为什么我的测试在我取出时不会失败:dependent => :从用户模型中删除
我的测试
@user.follow!(@followed)
@followed.destroy
@user.followers.should_not include(@followed)
我的模型,没有:dependent => :破坏
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 :(得分:0)
您的测试逻辑中似乎存在错误....
#1 @followed.destroy
#2 @user.followers.should_not include(@followed)
你的代码的第一行#1,我认为@followed在你调用destroy时变为nil,而在第2行,@ 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