接收“未知方法”针对 ActiveRecord 模型运行 RSpec 测试时出现错误

发布于 2024-10-09 05:21:05 字数 1300 浏览 1 评论 0原文

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

I'm having a bit of trouble with some RSpec tests on some of my ActiveRecord validations. The test suite looks like this:

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

The model is set up so that it should pass all of these validations appropriately. The schema indicates that all of the fields I fill in are present and accounted for. Yet, when I run autotest, the tests fail with the following error:

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

I can create the very same @event instance in the console with those values, and it works perfectly. My gut reaction is that, for some reason, the model that the test suite is using doesn't know about the :event_abbr field, but I can't think why that might be. I'm sure I'm missing something, but I'm not sure what it is. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-10-16 05:21:05

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

RAILS_ENV=test rake db:migrate

否则,

rails console test

尝试一下那里。

Did you run your migrations on your test database? E.G.

RAILS_ENV=test rake db:migrate

else, try

rails console test

and try it there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文