Rspec - unknown attribute error

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.

Answers


Everything looks fine. Do you really have a column artist_id in your albums table ?


Need Your Help

Calling facts from database in prolog

prolog dcg prolog-assert

I've inserted the given context free grammar into the database using assert(....)

Dynamically Creating bootstrap-sliders with jquery or javascript

javascript jquery twitter-bootstrap

I'm trying this with no success. For reference, the bootstrap slider is here : http://seiyria.github.io/bootstrap-slider/.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.