有人知道如何在rspec中检查无效的枚举响应吗?我可以检查确保枚举不是空值而没有错误,但当我检查一个错误的枚举值时,就会出现错误。这是两个测试规范:
it 'is not valid without question type' do
expect(build(:question, question_type: nil)).to have(1).errors_on(:question_type)
end
it 'is not valid with a bad question type' do
expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type)
end
这是我的模型外观:
class Question < ActiveRecord::Base
enum question_type: [ :multiple_choice ]
validates :question_type, presence: true
end
这是错误信息:
Failure/Error: expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type)
ArgumentError:
'telepathy' is not a valid question_type
这是我的工厂:
FactoryGirl.define do
factory :question, :class => 'Question' do
question_type :multiple_choice
end
end
谢谢你的帮助!