※当方、rails5から入った初心者です。ですのでruby自体も初心者です。
###前提・実現したいこと
お問い合わせフォームのバリデーションのテストうち、モデルにない属性(確認ボタン)のバリデーションがうまくいかないです。
お問い合わせフォームは以下の要素を持っています。
- 名前
- メールアドレス
- 本文
- 確認ボタン
- 送信ボタン
DBに保存するのは、名前、メールアドレス、本文のみです。
###該当のソースコード
※FactoryGirlの接頭辞は省略するように設定しています。
ruby
1# app/views/contacts/index.html.slim 2 3h1 お問い合わせ 4 5= form_for @contact do |f| 6 7 - if @contact.errors.any? 8 = "#{pluralize(@contact.errors.count, 'error')}" 9 ul 10 - @contact.errors.full_messages.each do |msg| 11 li = msg 12 13 = f.label :name, '名前' 14 = f.text_field :name 15 16 br 17 18 = f.label :email, 'メールアドレス' 19 = f.email_field :email 20 21 br 22 23 = f.label :body, '内容' 24 = f.text_area :body 25 26 br 27 28 = f.label :confirmation, '確認' 29 = f.check_box :confirmation 30 31 br 32 33 = f.submi
ruby
1# app/models/contact.rb 2 3class Contact < ApplicationRecord 4 attr_accessor :confirmation 5 validates :name, presence: true 6 VALIDE_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i 7 validates :email, presence: true, format: { with: VALIDE_EMAIL_REGEX } 8 validates :body, presence: true 9 validates :confirmation, acceptance: true 10end
ruby
1# db/migrate/2017040455604_create_contacts.rb 2 3class CreateContacts < ActiveRecord::Migration[5.0] 4 def change 5 create_table :contacts do |t| 6 t.string :name, :null => false, :limit => 200 7 t.string :email, :null => false, :limit => 200 8 t.text :body, :null => false 9 10 t.timestamps 11 end 12 end 13end
ruby
1# spec/factories/contacts.rb 2 3FactoryGirl.define do 4 factory :contact do 5 name "bcafe" 6 email "bcafe-to@example.com" 7 body "this is test." 8 9 factory :contact_with_empty_name do 10 name nil 11 end 12 13 factory :contact_with_empty_email do 14 email nil 15 end 16 17 factory :contact_with_empty_body do 18 body nil 19 end 20 21 factory :contact_with_empty_confirmation do 22 confirmation nil 23 end 24 end 25end
ruby
1# spec/models/contact_spec.rb 2 3require 'rails_helper' 4 5RSpec.describe Contact, type: :model do 6 describe 'validation' do 7 it 'is valid with name, email, body and confirmation' do 8 contact = build(:contact) 9 expect(contact).to be_valid 10 end 11 12 it 'is invalid without name' do 13 contact = build(:contact_with_empty_name) 14 contact.valid? 15 expect(contact.errors[:name].any?).to be true 16 end 17 18 it 'is invalid without email' do 19 contact = build(:contact_with_empty_email) 20 contact.valid? 21 expect(contact.errors[:email].any?).to be true 22 end 23 24 it 'is invalid without body' do 25 contact = build(:contact_with_empty_body) 26 contact.valid? 27 expect(contact.errors[:body].any?).to be true 28 end 29 30 it 'is invalid without confirmation' do 31 contact = build(:contact_with_empty_confirmation) 32 contact.valid? 33 expect(contact.errors[:confirmation].any?).to be true 34 end 35 end 36 37 describe 'database' do 38 it 'should save contact instance with no abnormality' do 39 assert build(:contact).save 40 end 41 42 it 'should not save contact instance with epmty name' do 43 contact = build(:contact_with_empty_name) 44 expect { contact.save!(validate: false) }.to raise_error ActiveRecord::StatementInvalid 45 end 46 47 it 'should not save contact instance wihout email' do 48 contact = build(:contact_with_empty_email) 49 expect { contact.save!(validate: false) }.to raise_error ActiveRecord::StatementInvalid 50 end 51 52 it 'should noe save contact instance withoud body' do 53 contact = build(:contact_with_empty_body) 54 expect { contact.save!(validate: false) }.to raise_error ActiveRecord::StatementInvalid 55 end 56 end 57end 58
###発生している問題・エラーメッセージ
Failures: 1) Contact validation is invalid without confirmation Failure/Error: expect(contact.errors[:confirmation].any?).to be true expected true got false # ./spec/models/contact_spec.rb:31:in `block (3 levels) in <top (required)>'
###補足情報(言語/FW/ツール等のバージョンなど)
- ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
- Rails 5.0.2
- RSpec 3.5.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/15 04:06
2017/04/15 05:28