在创建时触发更新条件验证

时间:2019-06-25 21:17:58

标签: ruby-on-rails rspec

我有一个Rails5应用程序,其中表Participant的列名为“ returning_couns”。

我正在使用Rspec来测试在创建初始记录时是否不需要该值。

在participant.rb中,我们有:

validates_inclusion_of :returning_couns, :in => [true, false ], message: 'You must indicate the returning counselor status.', on: :update

这是来自partner_spec.rb:

describe 'returning counselor tests' do  
   before :each do  
     @adult_attr = { 
       :gender => 'Male', :first_name => 'John', :last_name => 'Smith',
       .... # other attrs for valid Participant, without returning_couns
     }
   end

  it 'does not require returning_couns on create' do
      expect(Participant.create(@adult_attr)).to be_valid
  end
end

此规范失败,并显示消息

  

预期的#参与者ID:1,名字:“ John”,... returning_couns:nil,...性别:“男”有效,但出现错误:回国,您必须指明回国顾问的身份。

这是rspec日志文件的最终输出。

  (0.2ms)  BEGIN
  (0.3ms)  SAVEPOINT active_record_1
  Participant Create (1.0ms)  INSERT INTO "participants" ("first_name", "last_name", "address1", "city", "state", "zip", "email", "mobile_phone", "home_phone", "shirt_size", "grade", "grade_date", "special_needs", "created_at", "updated_at", "youth", "full_legal_name", "bg_check_option", "gender") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) RETURNING "id"  [["first_name", "John"], ["last_name", "Smith"], ["address1", "100 First Street"], ["city", "Our Town"], ["state", "CA"], ["zip", "90001"], ["email", "john.smith@gmail.com"], ["mobile_phone", "916-123-1234"], ["home_phone", "916-111-2222"], ["shirt_size", "XL"], ["grade", "23 or older"], ["grade_date", "1996-06-25"], ["special_needs", "Allergic to shellfish and tree nuts"], ["created_at", "2019-06-25 21:14:47.974226"], ["updated_at", "2019-06-25 21:14:47.974226"], ["youth", false], ["full_legal_name", "John H. Smith"], ["bg_check_option", 1], ["gender", "Male"]]
 (0.3ms)  RELEASE SAVEPOINT active_record_1
 (1.0ms)  ROLLBACK

在创建时,即使条件验证指定了on: :update,也需要returning_couns字段。知道我在这里缺少什么吗?

0 个答案:

没有答案