Add tests for dependents: destroy in the relationship model (chapter 11, Rails Tutorial exercise, 2nd Ed)

Pretty sure that these tests work correctly. Got them crashing by deleting the dependent :: destroy options in has_many: relationships and has_many: reverse_connections in user.rb.

I wanted to share what I did if someone else works through the Michael Hartl Rails Tutorial 2nd Edition, chapter 11 Exercises.

Several questions arose from this exercise (see bottom of this post). If anyone could help, that would be great.

Chapter 11, Exercise 1:

Add tests for the dependencies: destroy in the relationship model (Listing 11.4 and Listing 11.16), following the example in Listing 10.15.

Here is my test: specs / 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 

A couple of questions arose from this exercise:

Question 1:

My initial tests were relationships. should be_nil reverse_relationships.should be_nil

But, the implemented array was still returning despite the absence of the user. So, when the user does not exist and the association method is called, the result is an array? It's always like that?

Question 2:

I wanted to play around with deleting relationships and backlinks for the user in the rails console.

I tried this

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

How do I actually destroy relationships forever? It seems to be a good thing to know when learning from the console.

Thanks! I'm still pretty new to Rails

+7
source share
6 answers

I am also a ruby ​​/ noob rails.

Question 1: Search rubyonrails.org for has_many and he says

Returns an array of all related objects. An empty array is returned if it is not found.

On a side note, you can check for both zero and empty:

 relationships.present?.should be_false 

Question 2: The parameter user.relationships.destroy requires: id

 user.relationships.destroy '1' 
+3
source

Maybe you need smt like this

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

Thank you for posting your code with your question. I just wanted to post this as a comment, not an answer, but it seems like I can't yet. Anyway, I just wanted to add a small potential candidate to your tests, but from the point of view of other_user . The test is similar to follow / unfollow tests, so I hope it will not be too redundant, but it tests the relationships directly, not the followed_users and followers that pass through them.

 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
source

Ruby on Rails 2nd Edition Tutorial

Exercise 11.5.1 Add tests to destroy the relationships associated with this user.

This code works for me. I tried to follow the example in Listing 10.15 .

specifications / 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 } . . . . 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
source

Re: paul, the array of relations does not consist of users, therefore its include () must always be false, therefore the test is always green. Re: maria, it seems that the follow_users and followers methods will not return a user who does not exist, even if the relationship associated with him remains. Therefore, this test is never red.

another solution:

  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
source

The above answers work, but I suppose I would say it is less .: 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 you can leave:

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

but I throw it there for those who want to make sure that all the related destruction actions work.

0
source

All Articles