|
4 | 4 | include UnitTests::ApplicationConfigurationHelpers |
5 | 5 |
|
6 | 6 | context 'a model with a presence validation' do |
| 7 | + context 'passing multiple attributes' do |
| 8 | + it 'accepts' do |
| 9 | + model = define_model 'Example', attr1: :string, attr2: :string do |
| 10 | + validates_presence_of(:attr1) |
| 11 | + validates_presence_of(:attr2) |
| 12 | + end |
| 13 | + |
| 14 | + expect(model.new).to validate_presence_of(:attr1, :attr2) |
| 15 | + end |
| 16 | + |
| 17 | + it 'fails when used in the negative' do |
| 18 | + model = define_model 'Example', attr1: :string, attr2: :string do |
| 19 | + validates_presence_of(:attr1) |
| 20 | + end |
| 21 | + |
| 22 | + assertion = lambda do |
| 23 | + expect(model.new).not_to validate_presence_of(:attr1, :attr2) |
| 24 | + end |
| 25 | + |
| 26 | + message = <<-MESSAGE |
| 27 | +Expected Example not to validate that :attr1 cannot be empty/falsy, but |
| 28 | +this could not be proved. |
| 29 | + After setting :attr1 to ‹nil›, the matcher expected the Example to be |
| 30 | + valid, but it was invalid instead, producing these validation errors: |
| 31 | +
|
| 32 | + * attr1: ["can't be blank"] |
| 33 | + MESSAGE |
| 34 | + |
| 35 | + expect(&assertion).to fail_with_message(message) |
| 36 | + end |
| 37 | + |
| 38 | + it 'accepts when using qualifiers' do |
| 39 | + model = define_model 'Example', attr1: :string, attr2: :string do |
| 40 | + validates_presence_of(:attr1, allow_nil: true) |
| 41 | + validates_presence_of(:attr2, allow_nil: true) |
| 42 | + end |
| 43 | + |
| 44 | + expect(model.new).to validate_presence_of(:attr1, :attr2).allow_nil |
| 45 | + end |
| 46 | + |
| 47 | + it 'rejects when one attribute does not match the qualifier' do |
| 48 | + model = define_model 'Example', attr1: :string, attr2: :string do |
| 49 | + validates_presence_of(:attr1, allow_nil: true) |
| 50 | + validates_presence_of(:attr2) |
| 51 | + end |
| 52 | + |
| 53 | + assertion = lambda do |
| 54 | + expect(model.new).to validate_presence_of(:attr1, :attr2).allow_nil |
| 55 | + end |
| 56 | + |
| 57 | + message = <<-MESSAGE |
| 58 | +Expected Example to validate that :attr2 cannot be empty/falsy, but this |
| 59 | +could not be proved. |
| 60 | + After setting :attr2 to ‹nil›, the matcher expected the Example to be |
| 61 | + valid, but it was invalid instead, producing these validation errors: |
| 62 | +
|
| 63 | + * attr2: ["can't be blank"] |
| 64 | + MESSAGE |
| 65 | + |
| 66 | + expect(&assertion).to fail_with_message(message) |
| 67 | + end |
| 68 | + end |
| 69 | + |
7 | 70 | it 'accepts' do |
8 | 71 | expect(validating_presence).to matcher |
9 | 72 | end |
|
0 commit comments