Last active
July 7, 2018 14:25
-
-
Save yum45f/8aea652195ed02476bae3cd0dd269d9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContactForm | |
include ActiveModel::Model | |
include ActiveModel::Attributes | |
#include ActiveModel::Callbacks | |
attr_accessor :name, :email, :subject, :body, :agreement_privacy_policy | |
define_model_callbacks :save | |
validates :name, presence: true | |
validates :email, presence: true | |
validates :subject, presence: true | |
validates :body, presence: true | |
validates :agreement_privacy_policy, presence: true | |
validates_acceptance_of :agreement_privacy_policy, allow_nil: false | |
def save | |
run_callbacks :save do | |
return false if invalid? | |
@name = self.name | |
@email = self.email | |
@subject = self.subject | |
@body = self.body | |
bodyAttachments = {"title": "メンターお問いあわせ", | |
"text": "\n\n\n*Email* :\n" + @email + | |
"\n\n*お名前* :\n" + @name + | |
"\n\n*件名* :\n" + @subject + | |
"\n\n*本文* :\n" + @body | |
}; | |
massage_body = "<!channel> お問い合わせフォームより新しい通知が来ています! \n" | |
notifier = slack_notifer_client "#各種お問い合わせボード" | |
notifier.post attachments: [bodyAttachments], text: massage_body | |
true | |
end | |
end | |
private | |
def slack_notifer_client channel | |
hooks_url = ENV['SLACK_INCOMING_WEBHOOKS_URL'] | |
Slack::Notifier.new hooks_url, channel: channel | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe ContactForm, type: :model do | |
describe "#Save" do | |
let(:contact_form) { ContactForm.new(params) } | |
let(:params) { { name: name, email: email, subject: subject, body: body } } | |
let(:slack_notifer_client_mock) { double('Slack Notifer client') } | |
before do | |
allow(slack_notifer_client_mock).to receive(:post) | |
allow_any_instance_of(ContactForm).to receive(:slack_notifer_client).and_return(slack_notifer_client_mock) | |
end | |
before :each do | |
contact_form.save | |
end | |
context "Name が空欄の時" do | |
let(:name) { } | |
let(:email) { "[email protected]" } | |
let(:subject) { "Subject" } | |
let(:body) { "ABCDEF BODY" } | |
it "エラーを返すこと" do | |
Rails.logger.info contact_form | |
expect(contact_form.errors[:name]).to be_present | |
end | |
end | |
context "E-mail が空欄の時" do | |
let(:name) { "ABC NAME" } | |
let(:email) { } | |
let(:subject) { "Subject" } | |
let(:body) { "ABCDEF BODY" } | |
it "エラーを返すこと" do | |
expect(contact_form.errors[:email]).to be_present | |
end | |
end | |
context "Subject が空欄の時" do | |
let(:name) { "ABC NAME" } | |
let(:email) { "[email protected]" } | |
let(:subject) { } | |
let(:body) { "ABCDEF BODY" } | |
it "エラーを返すこと" do | |
expect(contact_form.errors[:subject]).to be_present | |
end | |
end | |
context "Body が空欄の時" do | |
let(:name) { "ABC NAME" } | |
let(:email) { "[email protected]" } | |
let(:subject) { "Subject" } | |
let(:body) { } | |
it "エラーを返すこと" do | |
Rails.logger.info contact_form | |
contact_form.save | |
expect(contact_form.errors[:body]).to be_present | |
end | |
end | |
context "全ての項目が埋まっている時" do | |
let(:name) { "ABC NAME" } | |
let(:email) { "[email protected]" } | |
let(:subject) { "Subject" } | |
let(:body) { "ABCDEF BODY" } | |
let(:massage_body) { "<!channel> お問い合わせフォームより新しい通知が来ています! \n" } | |
let(:bodyAttachments) { | |
{"title": "メンターお問いあわせ", | |
"text": "\n\n\n*Email* :\n" + email + | |
"\n\n*お名前* :\n" + name + | |
"\n\n*件名* :\n" + subject + | |
"\n\n*本文* :\n" + body | |
} | |
} | |
it "フォームが正しく送信されること" do | |
expect{ contact_form.save }.not_to raise_error | |
end | |
it "Slack に正しい情報が渡されること" do | |
expect(slack_notifer_client_mock).to receive(:post) | |
end | |
it "それぞれのインスタンス変数に正しい値が代入されていること" do | |
expect(contact_form.instance_variable_get :@name).to eq name | |
expect(contact_form.instance_variable_get :@email).to eq email | |
expect(contact_form.instance_variable_get :@subject).to eq subject | |
expect(contact_form.instance_variable_get :@body).to eq body | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle exec rspec spec | |
##### RESULT ##### | |
Failures: | |
1) ContactForm#Save 全ての項目が埋まっている時 Slack に正しい情報が渡されること | |
Failure/Error: expect(slack_notifer_client_mock).to receive(:post) | |
(Double "Slack Notifer client").post(*(any args)) | |
expected: 1 time with any arguments | |
received: 0 times with any arguments | |
# ./spec/models/contact_form_spec.rb:90:in `block (4 levels) in <top (required)>' | |
Finished in 1.36 seconds (files took 5.27 seconds to load) | |
45 examples, 1 failure, 15 pending | |
Failed examples: | |
rspec ./spec/models/contact_form_spec.rb:89 # ContactForm#Save 全ての項目が埋まっている時 Slack に正しい情報が渡されること |
答えは簡単だった。
class ContactForm
include ActiveModel::Model
# 略
attr_accessor :name, :email, :subject, :body, :agreement_privacy_policy
define_model_callbacks :save
validates :name, presence: true
validates :email, presence: true
validates :subject, presence: true
validates :body, presence: true
validates :agreement_privacy_policy, presence: true
validates_acceptance_of :agreement_privacy_policy, allow_nil: false
def save
# 後略
ここで、agreemnt_privacy_policy
を、 presence: true
かつ、 allow_nil: false
って指定してるのにもかかわらず、
require 'rails_helper'
RSpec.describe ContactForm, type: :model do
describe "#Save" do
let(:contact_form) { ContactForm.new(params) }
let(:params) { { name: name, email: email, subject: subject, body: body } }
let(:slack_notifer_client_mock) { double('Slack Notifer client') }
# 後略
Spec 側で、 let(:params) { { name: name, email: email, subject: subject, body: body } }
の部分にagreemnt_privacy_policy: true
としてなかったので、Model 側の save メソッドで、 return false if invalid?
の評価式ですでに false が返されてたっていうだけ。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
どこがだめ何だろうとか色々考えるけどわからない...
Mock? リアルだと正しく動くからな... 🤔 💭