You get undefined method 'full_name' for nil:NilClass
error because in @dish.author.full_name
, @dish.author
is nil i.e., the dish record doesn't have a matching author record.
What you need is to setup the factories such that when you create a dish its associated record also gets created :
FactoryGirl.define do
factory :dish, :class => 'Dish' do |f|
f.name "TestDish"
f.header "TestHeader"
f.avatar { File.open("spec/support/sample_photo.jpg")}
## f.author_id "1" Remove this line
association :author, factory: :assoc_author ## set association
end
factory :assoc_author, :class => 'Author' do |f|
## assign fields
f.first_name "John"
f.last_name "Doe"
end
## ...
end
This way when you call create(:dish)
, it will create a record in dishes
table plus an associated record in authors
table.