2017-06-04 83 views
0

我剛剛開始使用Rails(使用Rspec和Shoulda Matchers)來構建演示博客。試圖瞭解validates_presence_of測試失敗的原因

我真的剛剛開始,我的第一個測試失敗,但我不明白爲什麼。

我想我已經正確地設置了一切,但是當我嘗試驗證一個標題出現在我的文章模式,而是返回了失敗

Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError: 
    The matcher attempted to set :title on the Article to nil, but that 
    attribute does not exist. 

我的模型看起來像這樣...

class Article < ApplicationRecord 
    # attr_accessor :title 
    validates_presence_of :title 
end 

和我的測試...

require 'rails_helper' 

RSpec.describe Article do 
    it { should validate_presence_of :title } 
end 

如果我取消了attr_accessor中那麼測試通過,但我明白,這不需要與Rails。

我在做什麼錯?

回答

0

不需要attr_accessor,只要您的articles數據庫表具有title列。

您可以檢查db/schema.rb

+0

完美謝謝@ hugh-middleton,我已經更新了我的遷移文件,但沒有意識到該模式。不知道它從哪裏讀取屬性。 – bencarter78

+0

不用擔心。是的,只要你正在運行你的遷移,'schema.rb'會自動更新。 –