|
10 | 10 |
|
11 | 11 | it_behaves_like "a question model" |
12 | 12 |
|
13 | | - context "when given an empty string or nil" do |
14 | | - it "returns invalid with blank email" do |
15 | | - expect(question).not_to be_valid |
16 | | - expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.blank")) |
17 | | - end |
| 13 | + shared_examples "format validations" do |
| 14 | + context "when given a valid email address" do |
| 15 | + let(:email) { Faker::Internet.email } |
18 | 16 |
|
19 | | - it "returns invalid with empty string" do |
20 | | - question.email = "" |
21 | | - expect(question).not_to be_valid |
22 | | - expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.blank")) |
23 | | - expect(question.errors[:email]).not_to include(I18n.t("activemodel.errors.models.question/email.attributes.email.invalid_email")) |
24 | | - end |
| 17 | + before do |
| 18 | + question.email = email |
| 19 | + end |
25 | 20 |
|
26 | | - it "shows as a blank string" do |
27 | | - expect(question.show_answer).to eq "" |
| 21 | + it "is valid" do |
| 22 | + expect(question).to be_valid |
| 23 | + end |
| 24 | + |
| 25 | + it "is included in show_answer" do |
| 26 | + expect(question.show_answer).to eq email |
| 27 | + end |
| 28 | + |
| 29 | + it "returns the email address in show_answer_in_csv" do |
| 30 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, email]) |
| 31 | + end |
28 | 32 | end |
29 | 33 |
|
30 | | - it "returns a hash with an blank value for show_answer_in_csv" do |
31 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, ""]) |
| 34 | + context "when given an invalid email address" do |
| 35 | + it "is invalid" do |
| 36 | + question.email = "no-tld@domain" |
| 37 | + expect(question).not_to be_valid |
| 38 | + expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.invalid_email")) |
| 39 | + end |
32 | 40 | end |
33 | 41 | end |
34 | 42 |
|
35 | | - context "when given a string with an @ symbol in" do |
36 | | - before do |
37 | | - question.email = " @ " |
38 | | - end |
| 43 | + context "when question is mandatory" do |
| 44 | + context "when given an empty string or nil" do |
| 45 | + it "returns invalid with blank email" do |
| 46 | + expect(question).not_to be_valid |
| 47 | + expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.blank")) |
| 48 | + end |
39 | 49 |
|
40 | | - it "validates" do |
41 | | - expect(question).to be_valid |
42 | | - end |
| 50 | + it "returns invalid with empty string" do |
| 51 | + question.email = "" |
| 52 | + expect(question).not_to be_valid |
| 53 | + expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.blank")) |
| 54 | + expect(question.errors[:email]).not_to include(I18n.t("activemodel.errors.models.question/email.attributes.email.invalid_email")) |
| 55 | + end |
43 | 56 |
|
44 | | - it "is included in show_answer" do |
45 | | - expect(question.show_answer).to eq " @ " |
46 | | - end |
| 57 | + it "shows as a blank string" do |
| 58 | + expect(question.show_answer).to eq "" |
| 59 | + end |
47 | 60 |
|
48 | | - it "returns the email address in show_answer_in_csv" do |
49 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, " @ "]) |
| 61 | + it "returns a hash with an blank value for show_answer_in_csv" do |
| 62 | + expect(question.show_answer_in_csv).to eq(Hash[question_text, ""]) |
| 63 | + end |
50 | 64 | end |
51 | | - end |
52 | 65 |
|
53 | | - context "when given a string without an @ symbol in" do |
54 | | - it "does not validate an address without an @" do |
55 | | - question.email = "not an email address" |
56 | | - expect(question).not_to be_valid |
57 | | - expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.invalid_email")) |
58 | | - end |
| 66 | + include_examples "format validations" |
59 | 67 | end |
60 | 68 |
|
61 | 69 | context "when question is optional" do |
|
81 | 89 | expect(question.show_answer_in_csv).to eq(Hash[question_text, ""]) |
82 | 90 | end |
83 | 91 |
|
84 | | - context "when given a string with an @ symbol in" do |
85 | | - before do |
86 | | - question.email = " @ " |
87 | | - end |
88 | | - |
89 | | - it "validates" do |
90 | | - expect(question).to be_valid |
91 | | - end |
92 | | - |
93 | | - it "is included in show_answer" do |
94 | | - expect(question.show_answer).to eq " @ " |
95 | | - end |
96 | | - |
97 | | - it "returns the email address in show_answer_in_csv" do |
98 | | - expect(question.show_answer_in_csv).to eq(Hash[question_text, " @ "]) |
99 | | - end |
100 | | - end |
101 | | - |
102 | | - context "when given a string without an @ symbol in" do |
103 | | - it "does not validate an address without an @" do |
104 | | - question.email = "not an email address" |
105 | | - expect(question).not_to be_valid |
106 | | - expect(question.errors[:email]).to include(I18n.t("activemodel.errors.models.question/email.attributes.email.invalid_email")) |
107 | | - end |
108 | | - end |
| 92 | + include_examples "format validations" |
109 | 93 | end |
110 | 94 | end |
0 commit comments