7

很確定這些測試工作正常。通過刪除user_rb中的has_many:關係和has_many:reverse_relationships上的dependent::destroy選項來使它們失敗。在關係模型(第11章練習1 Rails教程,第2版)中添加依賴關係的測試:

想分享我的情況下,其他人正在通過Michael Hartl's Rails Tutorial 2nd Edition, Chapter 11 Exercises.

的幾個問題,從這個練習起來沒有(看到這個帖子的底部)。如果任何人都可以提供幫助,那會很棒。

第11章,練習1:

添加試驗依賴性:破壞關係模型(清單11.4和清單11.16)由下列清單10.15中的示例。

這裏是我的測試: 規格/型號/ user_spec.rb

require 'spec_helper' 

describe User do 

    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        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.shou ld be_nil reverse_relationships.should be_nil

但是,儘管沒有用戶存在,但實現了一個數組仍然被返回。 因此,當用戶不存在並且調用關聯方法時,結果仍然是一個數組?這總是如此嗎?

問題2:

我想玩弄刪除關係和reverse_relationships在軌控制檯的用戶。

我想這

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

實際上,我怎麼永久破壞的關係?在控制檯中探索時似乎很好理解。

謝謝!我還是很新的Rails

回答

3

我是一個紅寶石/鐵軌noob太。

問題1: 搜索rubyonrails.orghas_many和它說

返回所有相關對象的數組。如果沒有找到,則返回一個空數組。

在一個側面說明,你可以測試未繳和空:

relationships.present?.should be_false 

問題2: 的user.relationships.destroy需要:ID

user.relationships.destroy '1' 
+0

感謝您的幫助埃裏克! – 2012-04-24 20:50:29

0

感謝您與發佈您的代碼你的問題。我只想發表這個評論而不是答案,但似乎我還不能。無論如何,我只想給你的測試增加一個小的潛在候選人,但從other_user的角度來看。測試類似於關注/取消關注測試,所以希望它不會太多,但它直接測試relationships,而不是測試followed_users和。

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 
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: "[email protected]", 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 
0

回覆:保羅,關係陣列不是由用戶構成的,所以他的包括()應該永遠是假的,所以測試總是綠色。 回覆:瑪麗亞,似乎follow_users和追隨者的方法不會返回一個不存在的用戶,即使他們之間的關係依然存在。所以這個測試從來都不是紅色的。

另一種解決方案:

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 
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 

但我把它扔在那裏的人想要確保所有相關的銷燬行動在工作。

3

可能是你需要的SMT這樣

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