summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/email_format_shared_examples.rb
blob: 22d6c2b38e3a814eef888ec3183e6a980ddf38d5 (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
# frozen_string_literal: true

# Specifications for behavior common to all objects with an email attribute.
# Takes a list of email-format attributes and requires:
# - subject { "the object with a attribute= setter"  }
#   Note: You have access to `email_value` which is the email address value
#         being currently tested).

shared_examples 'an object with email-formated attributes' do |*attributes|
  attributes.each do |attribute|
    describe "specifically its :#{attribute} attribute" do
      %w[
        info@example.com
        info+test@example.com
        o'reilly@example.com
        mailto:test@example.com
        lol!'+=?><#$%^&*()@gmail.com
      ].each do |valid_email|
        context "with a value of '#{valid_email}'" do
          let(:email_value) { valid_email }

          it 'is valid' do
            subject.send("#{attribute}=", valid_email)

            expect(subject).to be_valid
          end
        end
      end

      %w[
        foobar
        test@test@example.com
      ].each do |invalid_email|
        context "with a value of '#{invalid_email}'" do
          let(:email_value) { invalid_email }

          it 'is invalid' do
            subject.send("#{attribute}=", invalid_email)

            expect(subject).to be_invalid
          end
        end
      end
    end
  end
end