I followed the Restauranlty tutorial at rubyonrailstutor.com and everything worked perfectly. Now I'm trying to implement the same tests in my project, which has more models with associations between them and I'm getting errors.
I've tried researching this problem, and even came across a few questions on this site - most notably this one and this one.
I have the following models:
class Album < ActiveRecord::Base
belongs_to :artist
validates_presence_of :title
validates_presence_of :no_of_tracks
end
class Artist < ActiveRecord::Base
has_many :albums
end
And I have the following factories:
FactoryGirl.define do
factory :artist do
name "MyString"
image "MyString"
bio "MyString"
end
end
FactoryGirl.define do
factory :album do
title "MyString"
no_of_tracks 0
artist
end
end
Apart from following the Restaurantly tutorial, this is my first time testing so I also looked at this guide for setting up factory girl for models with associations.
My test is:
describe Album do
subject(:album) { FactoryGirl.build :album, title: nil, artist: nil,
no_of_tracks: nil }
it { expect(album.valid?).to be_false }
end
But when I run it it fails with the error: can't write unknown attribute artist_id.
The questions I mentioned above suggested that perhaps my database has not been properly migrated but I've run both
bundle exec rake db:migrate RAILS_ENV=test
and
bundle exec rake db:test:prepare
to no avail. As I said, I'm very, very new to testing so if anyone could point me in the right direction I'd be grateful.