Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 在关系模型中为dependent:destroy添加测试(第11章,练习1 Rails教程,第2版)_Ruby On Rails 3_Railstutorial.org_Rails Console_Dependent Destroy - Fatal编程技术网

Ruby on rails 3 在关系模型中为dependent:destroy添加测试(第11章,练习1 Rails教程,第2版)

Ruby on rails 3 在关系模型中为dependent:destroy添加测试(第11章,练习1 Rails教程,第2版),ruby-on-rails-3,railstutorial.org,rails-console,dependent-destroy,Ruby On Rails 3,Railstutorial.org,Rails Console,Dependent Destroy,非常确定这些测试工作正常。通过删除user.rb中has_many:relationships和has_many:reverse_relationships上的dependent::destroy选项,使它们失败 想和大家分享一下我所做的事情,以防有人正在处理 从这个练习中产生了一些问题(见本文底部)。如果有人能帮忙,那就太好了 第11章练习1: 按照清单10.15中的示例,在关系模型(清单11.4和清单11.16)中添加dependent:destroy测试 这是我的测试: 规格/型号/用户规

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

想和大家分享一下我所做的事情,以防有人正在处理

从这个练习中产生了一些问题(见本文底部)。如果有人能帮忙,那就太好了

第11章练习1:

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

这是我的测试: 规格/型号/用户规格rb


这一练习引发了几个问题:

问题1:

我最初的测试是 关系。应为零 反向关系应为零

但是,意识到尽管没有用户,仍然返回了一个数组。 那么,当用户不存在并且调用了关联方法时,结果仍然是一个数组?这总是真的吗

问题2:

我想在rails控制台中为用户处理删除关系和反向_关系的问题

我试过这个

> user = User.first
> user.relationships
 # returns a bunch of relationships
> user.relationships.destroy
=> []
> user.relationships
 # returns same bunch of relationships
我如何真正永久地破坏关系?在控制台中进行探索时,应该知道这一点


谢谢!我对Rails还是相当陌生的

我也是ruby/Rails的noob

问题1: 搜索的
有很多
,上面写着

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

另一方面,您可以测试nil和empty:

relationships.present?.should be_false
问题2:
user.relationships.destroy
需要:id

user.relationships.destroy '1'

感谢您在问题中发布代码。我只想把这篇文章作为评论而不是回答,但似乎我还不能。无论如何,我只是想在你的测试中添加一个小的潜在候选人,但是从
其他用户的角度来看。该测试类似于follow/unfollow测试,因此希望它不会太冗余,但它直接测试
关系
,而不是跟踪用户
和跟随者

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

练习11.5.1添加用于破坏与给定用户关联的关系的测试

这个代码对我有用。我试着效仿这个例子

规格/型号/用户规格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

Re:paul,relationships数组不是由用户组成的,所以他的include()应该总是false,所以测试总是绿色的。 Re:maria,看来followed_users and 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

以上答案有效,但我想我会分享我的答案,因为它较短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

但是我把它放在那里是为了那些想确保所有相关的销毁操作都在工作的人。

也许你需要这样的smt

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