Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot

Introduction

As I was working on the Rails tutorial with rspec support, I was addicted to creating Relationship test data for the user in Listing 14.28 in 14.2.3 with FactoryBot, so I'll post it as a reminder.

environment

Thing you want to do

(1) Define the fixture that creates the relationship test data in FactoryBot as shown below. (2) Create user test data in the FactoryBot definition at the same time.

ruby:test/fixtures/relationships.yml(Listing 14.28)


one:
  follower: michael
  followed: lana

two:
  follower: michael
  followed: malory

three:
  follower: lana
  followed: michael

four:
  follower: archer
  followed: michael

Solutions

Give it a try and list the solutions you have adopted.

Part 1 Define a Factory that creates Relationship test data (solve only ①)

This is the most straightforward method. Create a factory for the Relationship class with follower_id and followed_id. However, I couldn't set the association well by this method, so I created the user test data separately.

spec/factories/relationships.rb


 factory :relationship, class: Relationship do
    follower_id { follower.id }
    followed_id { followed.id }
  end

spec/models/relationship_spec.rb


RSpec.describe Relationship, type: :model do
  let(:follower) { create(:user) }
  let(:followed) { create(:user) }
  let(:relationship) { create(:relationship, follower: follower, followed: followed) }

  it { expect(relationship).to be_valid }
end

Part 2 Define FactoryBot that creates test data for users with Relationship (solve both ① and ②?)

The second method is to create test data for users with relationships. The user's test data does not need to have a relationship in every test, so we allow it to be set as needed in the trait. This way, you don't have to create user and relationship test data respectively. However, this method is just a method of creating test data of user, and it seems that it is not suitable for testing relationship because it is necessary to pass user to call relationship.

spec/factories/users.rb


factory :user do
  .
  .
  .
  trait :has_followed do
    after(:create) do |user|
      followed = create(:user)
      user.follow(followed) 
    end
  end
end

spec/models/relationship_spec.rb


RSpec.describe Relationship, type: :model do
  let(:user) { create(:user, :has_followed) }

  it { expect(user.actve_relationships).to be_valid }
end

At the end

In the end, I couldn't find a solution to completely achieve ①②, so I decided to write the relationship test in part 1 and the relationship user test in part 2.

References

-FactoryBot (FactoryGirl) Cheat Sheet -Create has_many through association with factory_bot -Only 5 things to remember when using FactoryBot

Recommended Posts

Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
Introduction to RSpec 4. Create test data with Factory Bot
rails tutorial Chapter 6
Rails tutorial test
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Memorandum (Chapter 3, 3.1)
[Rails] Test with RSpec
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails] Push transmission with LINE Bot
[Rails Tutorial Chapter 5] Create a layout
[Rails] Creating a new project with rails new
rails tutorial chapter 10 summary (for self-learning)
[Rails] Initial data creation with seed
Chewing Rails Tutorial [Chapter 2 Toy Application]
Initial data input with [Rails] seed_fu!
Rails Tutorial (4th Edition) Memo Chapter 6
Let's unit test with [rails] Rspec!
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
[Ruby on Rails] View test with RSpec
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
[Ruby on Rails] Controller test with RSpec
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
[Ruby on Rails] Model test with RSpec
Rails Tutorial 6th Edition Learning Summary Chapter 3
rails test fails with database reference error
Rails Tutorial 6th Edition Learning Summary Chapter 8