Rails 3到4迁移唯一性验证问题

时间:2017-02-10 20:08:04

标签: ruby-on-rails ruby validation rspec

上下文

我们正在从Rails 3.2.12迁移到4.0.2,从Ruby 1.9.3迁移到2.1.8。

我们有很多测试覆盖率来完成RSpec形式的迁移。

问题

检查卡模型上的唯一性验证失败的规范之一。

validates :mobile, uniqueness: {scope: :program_member_id, message: I18n.t('models.card.error.cardholder_already_has_mobile')}, if: :mobile

program_member可能只有一张mobile: true卡。

该规范为会员创建了2张卡片,将其中一张卡片转换为移动卡片,然后在使用第二张卡片时预期验证消息。

let(:program) { FactoryGirl.create(:program) }
let(:card) { FactoryGirl.create(:card, program: program) }

context 'when cardholder already has a mobile card' do
  it 'fails validation' do
    card2 = FactoryGirl.create(:card, program: program)
    program_member_user = FactoryGirl.create(:program_member_user, card_number: card2.cardnumber)
    program_member = program_member_user.program_members.first

    program_member.cards << card2
    card2.update_attributes(:mobile => true)

    program_member.cards << card
    card.update_attributes(:mobile => true)

    expect(card.errors.messages).to include(:mobile=>[I18n.t('models.card.error.cardholder_already_has_mobile')])
  end
end

期望:

expected {} to include {:mobile=>["Cardholder already has a mobile card"]}

当我转到我们的master分支时,此规范通过。

从此规范工作到失败的唯一因素是Rails 3到4迁移。

尝试在控制台中运行规范代码,但发现该成员有2张移动卡,并且card.valid?为这两个实例返回true

问题

Rails 4在唯一性验证或验证生命周期方面有什么变化吗?

1 个答案:

答案 0 :(得分:1)

好吧,我就可以了。

我使用相同的Ruby和Rails版本创建了一个测试项目。

https://github.com/frank184/test_uniquness

在这个项目中,我会有一个User模型,其中admin列为布尔值,具有类似的验证。

validates_uniqueness_of :admin, if: :admin?

我使用了should-matcher和rspec来描述所需的结果。

require 'rails_helper'

RSpec.describe User, type: :model do
  let(:user) { build :user }
  subject { user }

  describe 'validations' do
    context 'when admin = true' do
      before(:each) { user.admin = true }
      it { is_expected.to validate_uniqueness_of(:admin)  }
    end
  end
end

规范因以下输出而失败:

Failures:

  1) User validations when admin = true should validate that :admin is case-sensitively unique
     Failure/Error: it { is_expected.to validate_uniqueness_of(:admin)  }

       User did not properly validate that :admin is case-sensitively unique.
         After taking the given User, whose :admin is ‹true›, and saving it as
         the existing record, then making a new User and setting its :admin to
         ‹true› as well, the matcher expected the new User to be invalid, but
         it was valid instead.
     # ./spec/models/user_spec.rb:10:in `block (4 levels) in <top (required)>'

Finished in 0.11435 seconds (files took 0.79997 seconds to load)
1 example, 1 failure

我认为代码很好并且Rails完全碰到了4.1.0。

规范通过了!

bundle update
rspec
.

Finished in 0.09538 seconds (files took 1.28 seconds to load)
1 example, 0 failures