【发布时间】:2016-09-05 07:47:10
【问题描述】:
我使用 rspec-rails 和 shoulda-matcher 来测试我的模型。代码如下:
user_ticket.rb
class UserTicket < ActiveRecord::Base
belongs_to :user
belongs_to :ticket
enum relation_type: %w( creator supporter )
validates_uniqueness_of :relation_type, scope: [:user_id, :ticket_id]
end
user_ticket_spec.rb
RSpec.describe UserTicket, type: :model do
subject { FactoryGirl.build(:user_ticket) }
describe 'Relations' do
it { should belong_to(:user) }
it { should belong_to(:ticket) }
end
describe 'Validations' do
it { should define_enum_for(:relation_type).with(%w( creator supporter )) }
# PROBLEM HERE
it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
end
end
当我运行测试用例时,结果总是:
Failure/Error: it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
ArgumentError:
'CREATOR' is not a valid relation_type
我只是认为应该匹配器想要使用某些relation_type 值来验证唯一性:大写、小写等。我的问题是在这种情况下,如何通过定义的模型验证使测试通过?
【问题讨论】:
标签: ruby-on-rails rspec shoulda