1

我在一些 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 字段,但我想不出为什么会这样。我确定我错过了一些东西,但我不确定它是什么。任何帮助将不胜感激。

4

1 回答 1

2

您是否在测试数据库上运行了迁移?例如

RAILS_ENV=test rake db:migrate

否则,试试

rails console test

并在那里尝试。

于 2010-12-23T03:04:41.280 回答