Rspec - 未知属性错误
Rspec - unknown attribute error
我遵循了 rubyonrailstutor.com 上的 Restauranlty 教程,并且一切正常。现在,我正尝试在我的项目中实施相同的测试,该项目具有更多模型以及它们之间的关联,但我遇到了错误。
我已经尝试研究这个问题,甚至在这个网站上遇到了一些问题——最著名的是 this one and this one。
我有以下型号:
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
我有以下工厂:
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
除了遵循 Restaurantly 教程之外,这是我第一次测试,所以我还查看了 this guide 为具有关联的模型设置工厂女孩。
我的测试是:
describe Album do
subject(:album) { FactoryGirl.build :album, title: nil, artist: nil,
no_of_tracks: nil }
it { expect(album.valid?).to be_false }
end
但是当我 运行 它失败并显示错误:can't write unknown attribute artist_id
。
我上面提到的问题表明,也许我的数据库没有正确迁移,但我 运行 都
bundle exec rake db:migrate RAILS_ENV=test
和
bundle exec rake db:test:prepare
无济于事。正如我所说,我对测试非常非常陌生,所以如果有人能指出我正确的方向,我将不胜感激。
我怀疑你数据库中的 albums
table 没有 artist_id
外键。
一切看起来都很好。
您的 albums
table 中真的有 artist_id
列吗?
我遵循了 rubyonrailstutor.com 上的 Restauranlty 教程,并且一切正常。现在,我正尝试在我的项目中实施相同的测试,该项目具有更多模型以及它们之间的关联,但我遇到了错误。
我已经尝试研究这个问题,甚至在这个网站上遇到了一些问题——最著名的是 this one and this one。
我有以下型号:
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
我有以下工厂:
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
除了遵循 Restaurantly 教程之外,这是我第一次测试,所以我还查看了 this guide 为具有关联的模型设置工厂女孩。
我的测试是:
describe Album do
subject(:album) { FactoryGirl.build :album, title: nil, artist: nil,
no_of_tracks: nil }
it { expect(album.valid?).to be_false }
end
但是当我 运行 它失败并显示错误:can't write unknown attribute artist_id
。
我上面提到的问题表明,也许我的数据库没有正确迁移,但我 运行 都
bundle exec rake db:migrate RAILS_ENV=test
和
bundle exec rake db:test:prepare
无济于事。正如我所说,我对测试非常非常陌生,所以如果有人能指出我正确的方向,我将不胜感激。
我怀疑你数据库中的 albums
table 没有 artist_id
外键。
一切看起来都很好。
您的 albums
table 中真的有 artist_id
列吗?