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

Commit 528403b

Browse files
author
Yuma Soga
committed
Squashed commit of the following(Update Spec for new desgins):
commit 87f649da096edb6aa469faf8339b4a762a3402ca Author: Yuma Soga <ysoga19@gmail.com> Date: Sat Jul 7 22:52:13 2018 +0900 Update Spec for New desgin for Seinding 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 528403b

File tree

3 files changed

+119
-5
lines changed

3 files changed

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