接收“未知方法”针对 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 字段,但我无法想象为什么会这样。我确信我错过了一些东西,但我不确定它是什么。任何帮助将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在测试数据库上运行了迁移? EG
否则,
尝试一下那里。
Did you run your migrations on your test database? E.G.
else, try
and try it there.