7

很确定这些测试工作正常。通过删除 user.rb 中 has_many :relationships 和 has_many :reverse_relationships 上的依赖: :destroy 选项让它们失败。

想分享我所做的,以防其他人正在阅读Michael Hartl 的 Rails 教程第 2 版第 11 章练习。

这个练习产生了一些问题(见这篇文章的底部)。如果有人可以提供帮助,那就太好了。

第 11 章,练习 1:

按照清单 10.15 中的示例,在关系模型(清单 11.4 和清单 11.16)中添加依赖 :destroy 的测试。

这是我的测试: spec/models/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

这个练习提出了几个问题:

问题一:

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

但是,尽管没有用户存在,但仍意识到数组仍在返回。那么,当用户不存在,调用关联方法时,结果还是数组?这总是正确的吗?

问题2:

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

我试过这个

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

我如何真正永久地破坏关系?在控制台中探索似乎是一件好事。

谢谢!我对 Rails 还是很陌生

4

6 回答 6

4

可能你需要这样的 smt

it { should have_many(:relationships).dependent(:destroy) }
于 2014-08-29T09:20:04.437 回答
3

我也是一个 ruby​​/rails 菜鸟。

问题 1:搜索ruby ​​onrails.orghas_many并显示

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

附带说明一下,您可以同时测试 nil 和空:

relationships.present?.should be_false

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

user.relationships.destroy '1'
于 2012-04-24T18:23:53.167 回答
0

感谢您发布带有问题的代码。我只想将此作为评论而不是答案发布,但似乎我还不能。无论如何,我只是想在您的测试中添加一个小的潜在候选人,但从other_user's 的角度来看。该测试类似于关注/取消关注测试,所以希望它不会太多余,但它直接测试而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
于 2012-06-10T21:04:21.050 回答
0

Ruby on Rails 教程第 2 版。

练习 11.5.1添加测试以破坏与给定用户相关的关系。

这段代码对我有用。我尝试按照示例清单 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
于 2012-08-25T14:52:43.823 回答
0

回复:保罗,关系数组不是由用户构成的,所以他的include()应该总是为假,所以测试总是绿色的。回复:玛丽亚,即使引用他或她的关系仍然存在,followed_users 和 follower 方法似乎也不会返回不存在的用户。所以这个测试也从不红。

另一种解决方案:

  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
于 2013-10-12T07:43:16.267 回答
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

PS你可以省略:

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

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

于 2013-11-18T01:52:20.320 回答