[RSpec] Factorybot transient & trait with argument

n350071

n350071πŸ‡―πŸ‡΅

Posted on November 7, 2019

[RSpec] Factorybot transient & trait with argument

πŸ€” Situation

You want to make a user with a book that title is 'Ruby'.



class User
  has_many: books
end
class Book
  belongs_to: user
end


Enter fullscreen mode Exit fullscreen mode

πŸ‘ Solution

  • trait: you can run the process
  • trait with arguments: It is DEPRECATED, we should use transient instead.
  • transient: you can define and pass arguments with the block


FactoryBot.define do
  factory :user, class: User do
    trait :with_book
      transient do
        # πŸ¦„1. default value when you use :with_book trait
        # πŸ¦„2. Dont't assign just 'Agile'. see also: https://thoughtbot.com/blog/deprecating-static-attributes-in-factory_bot-4-11
        title { 'Agile' }
      end
      after(:build) do |user, evaluator|
        user.book = FactoryBot.create(:book, title: evaluator.title)
      end
    end
  end

  factory :book, class Book do
    sequence(:title) { |n| "book no.#{n}" } # πŸ¦„ default value
  end

end


Enter fullscreen mode Exit fullscreen mode


let!(:user) { create(:user, :with_book, title: 'Ruby') }


Enter fullscreen mode Exit fullscreen mode

πŸ”— Parent Note

πŸ’– πŸ’ͺ πŸ™… 🚩
n350071
n350071πŸ‡―πŸ‡΅

Posted on November 7, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related