2010-12-23 30 views
1

我在某些ActiveRecord驗證中遇到了一些RSpec測試問題。測試套件如下所示:針對ActiveRecord模型運行RSpec測試時收到「未知方法」錯誤

describe Event do 
    context "An Event" do 
    before do 
     valid_event_hash = { 
      :name => 'Blah Blah', 
      :desc => 'Yadda Yadda Yadda', 
      :category => 'some category', 
      :has_partner => false, 
      :event_abbr => 'BB' 
     } 
     @event = Event.new(valid_event_hash) 
    end 

    it "should have a name" do 
     @event.name = '' 
     @event.should_not be_valid 
    end 

    it "should have a description" do 
     @event.desc = '' 
     @event.should_not be_valid 
    end 

    it "should have an abbreviation no shorter than 2 letters and no longer than 3 letters" do 
     @event.event_abbr = '' 
     @event.should_not be_valid 
     @event.event_abbr = 'BlaBla' 
     @event.should_not be_valid 
     @event.event_abbr = 'B' 
     @event.should_not be_valid 
    end 

    after do 
     @event.destroy 
    end 
end 

end 

該模型設置爲使它能夠正確地通過所有這些驗證。該模式表明我填寫的所有字段都存在並佔據了。然而,當我運行自動測試,測試失敗,出現以下錯誤:

Failure/Error: @event = Event.new(valid_event_hash) 
unknown attribute: event_abbr 

我可以使用這些值控制檯創建非常相同的@event實例,它完美的作品。我的直覺反應是,出於某種原因,測試套件使用的模型不知道event_abbr字段,但我不明白爲什麼會這樣。我確定我錯過了一些東西,但我不確定它是什麼。任何幫助將不勝感激。

回答

2

您是否在測試數據庫上運行遷移?例如。

RAILS_ENV=test rake db:migrate 

否則,嘗試

rails console test 

,並嘗試它。

+2

我知道這是愚蠢的。我以爲我會同步這些數據庫......謝謝。 – 2010-12-23 03:17:37

相關問題