簡體   English   中英

RSpec-未知屬性錯誤

[英]Rspec - unknown attribute error

我在rubyonrailstutor.com上遵循了Restauranlty教程,並且一切正常。 現在,我正在嘗試在我的項目中實施相同的測試,該測試具有更多的模型,它們之間具有關聯,並且出現錯誤。

我嘗試研究此問題,甚至在此站點上遇到了幾個問題-最值得注意的是這個這個

我有以下型號:

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教程之外,這是我的首次測試,因此我還查看了本指南 ,以為帶有關聯的模型設置工廠女孩。

我的測試是:

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表格中是否確實有一列artist_id

我懷疑您數據庫中的albums表沒有artist_id foregin鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM