我有一个分类模型,其中包含以下内容:
# Mass assignable fields
attr_accessible :name, :classification, :is_shown
# Add validation
validates :name, :presence => true, :uniqueness => true
validates :classification, :presence => true
使用 rspec + capybara,我想测试唯一性验证器。
Taxonomy.create(:name => 'foo', :classification => 'activity').save!(:validate => false)
it { should validate_uniqueness_of(:name).scoped_to(:classification) }
此测试失败并出现以下错误:
Failure/Error: it { should validate_uniqueness_of(:name).scoped_to(:classification) }
Expected errors to include "has already been taken" when name is set to "foo", got errors: ["name has already been taken (\"foo\")", "classification is not included in the list (:activitz)"] (with different value of classification)
# ./spec/models/taxonomy_spec.rb:14:in `block (3 levels) in <top (required)>'
在我看来,测试应该通过了。我错过了什么?