blob: 1dbf3f66cfda31161279244236912147e8948cc2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# frozen_string_literal: true
require 'spec_helper'
describe DeviseEmailValidator do
let!(:user) { build(:user, public_email: 'test@example.com') }
subject { validator.validate(user) }
describe 'validations' do
context 'by default' do
let(:validator) { described_class.new(attributes: [:public_email]) }
it 'allows when email is valid' do
subject
expect(user.errors).to be_empty
end
it 'returns error when email is invalid' do
user.public_email = 'invalid'
subject
expect(user.errors).to be_present
expect(user.errors.first[1]).to eq 'is invalid'
end
it 'returns error when email is nil' do
user.public_email = nil
subject
expect(user.errors).to be_present
end
it 'returns error when email is blank' do
user.public_email = ''
subject
expect(user.errors).to be_present
expect(user.errors.first[1]).to eq 'is invalid'
end
end
end
context 'when regexp is set as Regexp' do
let(:validator) { described_class.new(attributes: [:public_email], regexp: /[0-9]/) }
it 'allows when value match' do
user.public_email = '1'
subject
expect(user.errors).to be_empty
end
it 'returns error when value does not match' do
subject
expect(user.errors).to be_present
end
end
context 'when regexp is set as String' do
it 'raise argument error' do
expect { described_class.new( { regexp: 'something' } ) }.to raise_error ArgumentError
end
end
context 'when allow_nil is set to true' do
let(:validator) { described_class.new(attributes: [:public_email], allow_nil: true) }
it 'allows when email is nil' do
user.public_email = nil
subject
expect(user.errors).to be_empty
end
end
context 'when allow_blank is set to true' do
let(:validator) { described_class.new(attributes: [:public_email], allow_blank: true) }
it 'allows when email is blank' do
user.public_email = ''
subject
expect(user.errors).to be_empty
end
end
end
|