上下文
我们正在从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"]}
当我去我们的主分公司时,这个规范通过.
从此规范工作到失败的唯一因素是Rails 3到4迁移.
尝试在控制台中运行规范代码只是为了找到该成员有2个移动卡并做card.valid?两个实例都返回true.
题
在Rails 4中,在唯一性验证或验证生命周期方面有什么变化吗?
@R_403_323@
好吧所以我正在做点什么.
我使用相同的Ruby和Rails版本创建了一个测试项目.
https://github.com/frank184/test_uniquness
在这个项目中,我会有一个User模型,其管理列为布尔值,具有类似的验证.
validates_uniqueness_of :admin,if: :admin?
我使用了shoulda-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