Skip to content
This repository was archived by the owner on Mar 19, 2020. It is now read-only.

Commit 3d47016

Browse files
author
Yuma Soga
committed
Squashed commit of the following(Update Specs for New desgin):
commit 46b508f3c774b00cc65c730335c0061489affed5 Author: Yuma Soga <ysoga19@gmail.com> Date: Sat Jul 7 22:52:13 2018 +0900 Update Spec for New desgin for Sending Contact form. commit 1996eac35aa3fbfab36341768f28cb887a34710d Author: Yuma Soga <ysoga19@gmail.com> Date: Sat Jul 7 17:35:02 2018 +0900 Update spec for instance variable of contact_form s commit 71d89ca5730810c5aaf793de41ae430aad45942e Author: Yuma Soga <ysoga19@gmail.com> Date: Sat Jul 7 17:15:05 2018 +0900 Add Spec dor new desgin
1 parent c35e5fa commit 3d47016

File tree

3 files changed

+116
-5
lines changed

3 files changed

+116
-5
lines changed

app/models/contact_form.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def save
2121
@subject = self.subject
2222
@body = self.body
2323

24-
bodyAttachments = {"title":"メンターお問いあわせ",
24+
bodyAttachments = {"title": "メンターお問いあわせ",
2525
"text": "\n\n\n*Email* :\n" + @email +
2626
"\n\n*お名前* :\n" + @name +
2727
"\n\n*件名* :\n" + @subject +
@@ -30,10 +30,14 @@ def save
3030

3131
massage_body = "<!channel> お問い合わせフォームより新しい通知が来ています! \n"
3232

33-
hooks_url = ENV['SLACK_INCOMING_WEBHOOKS_URL']
34-
notifier = Slack::Notifier.new hooks_url, channel: "#各種お問い合わせボード"
35-
notifier.post attachments: [bodyAttachments], text: massage_body
33+
slack_notifer_client.post attachments: [bodyAttachments], text: massage_body
3634
true
3735
end
3836
end
37+
38+
private
39+
def slack_notifer_client
40+
hooks_url = ENV['SLACK_INCOMING_WEBHOOKS_URL']
41+
Slack::Notifier.new hooks_url, channel: "#各種お問い合わせボード"
42+
end
3943
end

spec/models/contact_form_spec.rb

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,109 @@
11
require 'rails_helper'
22

33
RSpec.describe ContactForm, type: :model do
4-
pending "add some examples to (or delete) #{__FILE__}"
4+
describe "#Save" do
5+
6+
let(:contact_form) { ContactForm.new(params) }
7+
8+
let(:params) { { name: name, email: email, subject: subject, body: body, agreement_privacy_policy: true} }
9+
10+
11+
describe "バリデーション" do
12+
13+
before :each do
14+
contact_form.save
15+
end
16+
17+
context "Name が空欄の時" do
18+
let(:name) { }
19+
let(:email) { "ABE@TESTMAIL.COM" }
20+
let(:subject) { "Subject" }
21+
let(:body) { "ABCDEF BODY" }
22+
23+
it "エラーを返すこと" do
24+
Rails.logger.info contact_form
25+
expect(contact_form.errors[:name]).to be_present
26+
end
27+
end
28+
29+
context "E-mail が空欄の時" do
30+
let(:name) { "ABC NAME" }
31+
let(:email) { }
32+
let(:subject) { "Subject" }
33+
let(:body) { "ABCDEF BODY" }
34+
35+
it "エラーを返すこと" do
36+
expect(contact_form.errors[:email]).to be_present
37+
end
38+
end
39+
40+
context "Subject が空欄の時" do
41+
let(:name) { "ABC NAME" }
42+
let(:email) { "ABE@TESTMAIL.COM" }
43+
let(:subject) { }
44+
let(:body) { "ABCDEF BODY" }
45+
46+
it "エラーを返すこと" do
47+
expect(contact_form.errors[:subject]).to be_present
48+
end
49+
end
50+
51+
context "Body が空欄の時" do
52+
let(:name) { "ABC NAME" }
53+
let(:email) { "ABE@TESTMAIL.COM" }
54+
let(:subject) { "Subject" }
55+
let(:body) { }
56+
57+
it "エラーを返すこと" do
58+
Rails.logger.info contact_form
59+
contact_form.save
60+
expect(contact_form.errors[:body]).to be_present
61+
end
62+
end
63+
64+
end
65+
66+
describe "フォーム送信" do
67+
68+
context "全ての項目が埋まっている時" do
69+
let(:name) { "ABC NAME" }
70+
let(:email) { "ABE@TESTMAIL.COM" }
71+
let(:subject) { "Subject" }
72+
let(:body) { "ABCDEF BODY" }
73+
74+
let!(:slack_notifer_client_mock) { spy('Slack Notifer Client') }
75+
76+
let(:massage_body) { "<!channel> お問い合わせフォームより新しい通知が来ています! \n" }
77+
78+
let(:bodyAttachments) {
79+
{"title": "メンターお問いあわせ",
80+
"text": "\n\n\n*Email* :\n" + email +
81+
"\n\n*お名前* :\n" + name +
82+
"\n\n*件名* :\n" + subject +
83+
"\n\n*本文* :\n" + body
84+
}
85+
}
86+
87+
before do
88+
allow_any_instance_of(ContactForm).to receive(:slack_notifer_client).and_return(slack_notifer_client_mock)
89+
end
90+
91+
it "フォームがエラーなく送信されること" do
92+
expect{ contact_form.save }.not_to raise_error
93+
end
94+
95+
it "Slack に正しい情報が渡されること" do
96+
expect(slack_notifer_client_mock).to receive(:post)
97+
expect(contact_form.save).to be_truthy
98+
end
99+
100+
it "それぞれのインスタンス変数に正しい値が代入されていること" do
101+
expect(contact_form.instance_variable_get :@name).to eq name
102+
expect(contact_form.instance_variable_get :@email).to eq email
103+
expect(contact_form.instance_variable_get :@subject).to eq subject
104+
expect(contact_form.instance_variable_get :@body).to eq body
105+
end
106+
end
107+
end
108+
end
5109
end

spec/rails_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# Prevent database truncation if the environment is production
66
abort("The Rails environment is running in production mode!") if Rails.env.production?
77
require 'rspec/rails'
8+
9+
# Rails.logger = Logger.new(STDOUT)
10+
811
# Add additional requires below this line. Rails is not loaded until this point!
912

1013
# Requires supporting ruby files with custom matchers and macros, etc, in

0 commit comments

Comments
 (0)