If you have your factories configured as follows:
FactoryGirl.define do factory :library do name "Chicago Public Library" street_address "123 Morgan St." end factory :book do title "A Great Book" author "Mandy Yeats" association :library end factory :page do page_number 123 association :book end end
Whenever you instantiate a page: in your tests, FactoryGirl will also create an instance: book and: library. You do not need to create a library and a book first. If you create a book: a library will also be created for the test :. You can use these instances in your tests as follows:
page.book.library.name # after using FactoryGirl.create(:page) page.book.author book.library.street_address # after using FactoryGirl.create(:book)
This is the easiest way to do this that I found.
source share