我有一个简单的规范:
describe Setting do
subject { create(:setting) }
it { should be_valid }
end
与工厂:
FactoryGirl.define do
factory :setting do
key "some_key"
value "The appropriate value"
end
end
当我运行规范时:
rake spec SPEC=spec/models/setting_spec.rb
我收到以下错误:
1) Setting should be valid
Failure/Error: it { should be_valid }
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/models/setting_spec.rb:6:in `block (2 levels) in <top (required)>'
我尝试使用let(:setting)代替主题行并得到相同的错误。
编辑每条评论:
错误是指行it { should be_valid }
模型是:
class Setting < ActiveRecord::Base
attr_accessor :key, :value
validates_uniqueness_of :key
end
这是追踪:
# /var/lib/gems/1.9.1/gems/activerecord-3.2.16/lib/active_record/validations/uniqueness.rb:26:in `validate_each'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:153:in `block in validate'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:150:in `each'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:150:in `validate'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:310:in `_callback_before_1986'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:407:in `_run__3464288744618424855__validate__1974049388273351148__callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:405:in `__run_callback'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations.rb:228:in `run_validations!'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:403:in `_run__3464288744618424855__validation__1974049388273351148__callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:405:in `__run_callback'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations.rb:195:in `valid?'
# /var/lib/gems/1.9.1/gems/activerecord-3.2.16/lib/active_record/validations.rb:69:in `valid?'
# /var/lib/gems/1.9.1/gems/rspec-expectations-2.14.5/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/memoized_helpers.rb:68:in `should'
# ./spec/models/setting_spec.rb:6:in `block (2 levels) in <top (required)>'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `instance_eval'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `block in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:111:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:371:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:58:in `report'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:25:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:77:in `rescue in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:73:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
spec_helper.rb:http://pastebin.com/6fzDXnux
答案 0 :(得分:7)
此错误的直接原因是类派生自ActiveRecord :: Base,而ActiveRecord无法确定主键(AR要求验证唯一性)。最有可能的情况是,这是因为您没有运行迁移来创建settings
表,或者您创建了没有主键的settings
表。
违规行(基于提供的回溯):
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
请注意,to_sym
上正在调用finder_class.primary_key
。
答案 1 :(得分:1)
您也可以通过在模型中添加以下行来解决此问题。
self.primary_key = 'id'
e.g。
Class Person
self.primary_key = 'id'
end