升级到rails 4.2.1会导致validate_uniqueness_of规范失败

时间:2015-07-01 17:46:22

标签: ruby-on-rails ruby rspec ruby-on-rails-4.2

我有一个当前正在rails 4.1.4上运行的应用程序,我正试图将其升级到4.2.1

当涉及到validates_uniqueness_of声明时,我遇到了一些模型规范的问题

我的模特:

class PromoCode < ActiveRecord::Base
  validates_presence_of :expiration_date, :code, :discount
  validates_uniqueness_of :code
end

class Coupon < ActiveRecord::Base
  validates :code, presence: true, uniqueness: true
end

我的规格:

RSpec.describe PromoCode, type: :model do
  it { is_expected.to validate_presence_of(:expiration_date) }
  it { is_expected.to validate_presence_of(:code) }
  it { is_expected.to validate_presence_of(:discount) }
  it { is_expected.to validate_uniqueness_of(:code) }
end

RSpec.describe Coupon, type: :model do
  it { is_expected.to validate_presence_of(:code) }
  it { is_expected.to validate_uniqueness_of(:code) }
end

存在的规格传递正常,但对于两种模型,唯一性的规格都失败了,并且返回:

Failure/Error: it { is_expected.to validate_uniqueness_of(:code) }
  NoMethodError:
    undefined method `primary' for #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f99176e0f20>

这里发生了什么?

1 个答案:

答案 0 :(得分:0)

it { is_expected.to validate_uniqueness_of(:code) }

由shoulda matchers gem提供。看起来像个bug。将该gem更新为2.80+ stable应修复它。这里有一些细节

https://github.com/thoughtbot/shoulda-matchers/issues/639