在Relationship模型中添加依赖:destroy的测试(第11章,练习1 Rails教程,第2版)

时间:2012-04-20 21:58:24

标签: ruby-on-rails-3 railstutorial.org rails-console dependent-destroy

非常确定这些测试工作正常。通过删除has_many:relationships上的dependent :: destroy选项和user.rb中的has_many:reverse_relationships来解决它们失败。

想要分享我所做的事情以防其他人正在通过Michael Hartl's Rails Tutorial 2nd Edition, Chapter 11 Exercises.

这个练习产生了一些问题(见本文的底部)。如果有人能提供帮助,那就太棒了。

第11章,练习1:

按照清单10.15中的示例,在Relationship模型(清单11.4和清单11.16)中为dependent:destroy添加测试。

这是我的测试: 规格/模型/ user_spec.rb

require 'spec_helper'

describe User do

  before do
  @user = User.new(name: "Example User", email: "user@example.com", 
                   password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  [...code omitted...]

  describe "relationship associations" do
    let(:other_user) { FactoryGirl.create(:user) }
    before do
      @user.save
      @user.follow!(other_user)
      other_user.follow!(@user)
    end

    it "should destroy associated relationships" do
      relationships = @user.relationships
      @user.destroy
      relationships.should be_empty
    end

    it "should destroy associated reverse relationships" do
      reverse_relationships = @user.reverse_relationships
      @user.destroy
      reverse_relationships.should be_empty
    end
  end

这个练习产生了几个问题:

问题1:

我最初的测试是 relationships.should be_nil reverse_relationships.should be_nil

但是,尽管没有用户存在,但实现了数组仍在返回。 那么,当用户不存在并且调用关联方法时,结果仍然是数组?这总是如此吗?

问题2:

我想在rails控制台中为用户删除关系和reverse_relationship。

我试过这个

> user = User.first
> user.relationships
 # returns a bunch of relationships
> user.relationships.destroy
=> []
> user.relationships
 # returns same bunch of relationships

我如何永久地破坏关系?在控制台中探索时似乎很有用。

谢谢!我对Rails还是个新手

6 个答案:

答案 0 :(得分:3)

我也是ruby / rails noob。

问题1: 已搜索rubyonrails.org has_many,并说

  

返回所有关联对象的数组。如果没有找到,则返回一个空数组。

在旁注中,您可以测试nil和empty:

relationships.present?.should be_false

问题2: user.relationships.destroy需要:id

user.relationships.destroy '1'

答案 1 :(得分:3)

可能你需要这样的smt

it { should have_many(:relationships).dependent(:destroy) }

答案 2 :(得分:0)

感谢您发布带有问题的代码。我只是想发表这个评论而不是答案,但似乎我还不能。无论如何,我只想在你的测试中添加一个小的潜在候选人,但是从other_user的角度来看。该测试类似于follow / unfollow测试,所以希望它不会太冗余,但它会直接测试relationships,而不是通过它们的followed_usersfollowers

describe "relationship associations" do
  ...
  context "when a follower/followed user is destroyed" do
    subject { other_user }

    before { user.destroy }

    its(:relationships) { should_not include(user) }
    its(:reverse_relationships) { should_not include(user) }
  end
end

答案 3 :(得分:0)

Ruby on Rails Tutorial 2nd Edition.

练习11.5.1 添加测试以销毁与给定用户关联的关系。

此代码适用于我。我试图遵循示例Listing 10.15

<强>规格/模型/ user_spec.rb

require 'spec_helper'

describe User do

  before do 
    @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }
  .
  .
  .
  .
  describe "user relationships associations" do
    let (:other_user) { FactoryGirl.create(:user) }
    let (:another_user) { FactoryGirl.create(:user) }

    before do
      @user.save
      @user.follow!(other_user)
      @user.follow!(another_user)
      other_user.follow!(@user)
      other_user.follow!(another_user)
      another_user.follow!(@user)
      another_user.follow!(other_user)
    end

    its(:followed_users) { should include(other_user) }
    its(:followers) { should include(another_user) }

    it "should destroy associated followers" do
      followers = @user.followers
      @user.destroy
      followers.each do |follower|
        follower.followed_users.should_not include(@user)
      end
    end

    it "should destroy associated followed users" do
      followed_users = @user.followed_users
      @user.destroy
      followed_users.each do |followed_user|
        followed_user.followers.should_not include(@user)
      end
    end
  end
end

答案 4 :(得分:0)

Re:paul,关系数组不是由用户构成的,所以他的include()应该总是为false,所以测试总是绿色的。 Re:maria,看来follow_users和followers方法不会返回不存在的用户,即使他或她的关系仍然存在。所以这个测试也永远不会变红。

另一种解决方案:

  describe "relationships" do
    let(:other_user) { FactoryGirl.create(:user) }
    before do
      @user.save
      @user.follow!(other_user)
    end

    let(:relationship) { @user.relationships.last }

    describe "should be destroyed when the followed user is destroyed" do
      before { other_user.destroy }
      its(:relationships) { should_not include(relationship) }
    end

    describe "should be destroyed when the following user is destroyed" do
      subject { other_user }
      before { @user.destroy }
      its(:reverse_relationships) { should_not include(relationship) }
    end
  end

答案 5 :(得分:0)

以上答案有效,但我想我会分享它的更短......:D

describe "following" do

  let(:other_user) { FactoryGirl.create(:user) }
  before do
    @user.save
    @user.follow!(other_user)
    other_user.follow!(@user)
  end

   it { should be_following(other_user) }
   its(:followed_users) { should include(other_user) }

   it "should destroy associated followed_users and followers" do
     @user.destroy
     @user.relationships.present?.should be_false
     @user.reverse_relationships.present?.should be_false

     expect(other_user.followers).not_to include(@user)
     expect(other_user.followed_users).not_to include(@user)
   end
   .
   .
   .
   .
 end
end
你可以遗漏:

@user.relationships.present?.should be_false
@user.reverse_relationships.present?.should be_false

但是我把它扔给那些想要确保所有相关的破坏行动都在起作用的人。