如何让shoulda-matchers为字段使用特定的值集?

时间:2016-03-04 15:01:19

标签: ruby-on-rails rspec shoulda

我有一个优惠券模型,可以使用相同优惠券代码的多种货币。

所以我这样验证:

validates :code, presence: true, uniqueness: { case_sensitive: false, scope: :currency }, length: { in: 1..40 }

并测试它:

it { should validate_uniqueness_of(:code).case_insensitive.scoped_to(:currency) }

此外,优惠券还会重写货币属性以接受货币符号($)和ISO代码(USD)。它将符号重写为相应的ISO代码:

# Convert currency symbols to ISO code
def currency=(value)
  return write_attribute(:currency, nil) if value.blank?
  write_attribute(:currency, currency_to_iso(value.strip))
end

def currency_to_iso(symbol_or_iso)
  return unless symbol_or_iso
  (Money::Currency.find(symbol_or_iso) || Money::Currency.find_by_symbol(symbol_or_iso)).try(:iso_code)
end

当货币代码不正确时,它会转换为nil

所以,应该匹配产生错误:

   Coupon did not properly validate that :code is case-insensitively unique
   within the scope of :currency.
     After taking the given Coupon, whose :code is ‹"t8u"›, and saving it
     as the existing record, then making a new Coupon and setting its :code
     to ‹"t8u"› as well and its :currency to a different value, ‹"dummy
     value"› (read back as ‹nil›), the matcher expected the new Coupon to
     be valid, but it was invalid instead, producing these validation
     errors:

     * code: ["has already been taken"]

如何使shoulda-matches仅使用真实货币代码,例如FFaker::Currency或预定义列表?

1 个答案:

答案 0 :(得分:1)

我深入研究了shoulda代码并发现它使用了validate_uniqueness_of的预定义值集,并且无法更改它(除了黑客Shoulda::Matchers::Util类)。

def self.dummy_value_for(column_type, array: false)
  if array
    [dummy_value_for(column_type, array: false)]
  else
    case column_type
    when :integer
      0
    when :date
      Date.new(2100, 1, 1)
    when :datetime, :timestamp
      DateTime.new(2100, 1, 1)
    when :time
      Time.new(2100, 1, 1)
    when :uuid
      SecureRandom.uuid
    when :boolean
      true
    else
      'dummy value'
    end
  end
end

所以我构建了以下解决方法:

context 'coupon code should be unique for the same currency' do
  before { create(:coupon_amount, currency: 'USD', code: 'aaa') }
  it 'allows to create a coupon with the same code and another currency' do
    create(:coupon_amount, currency: 'EUR', code: 'aaa')
  end
  it 'does not allow to create a coupon with the same code and currency' do
    expect { create(:coupon_amount, currency: 'USD', code: 'aaa') }.to(
      raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Code has already been taken')
    )
  end
end

它使用正确的价值来验证唯一性,并且像魅力一样有效。