前提・実現したいこと
簡易的なアウトプットアプリを作成しています。
post(投稿機能)のテスト実行時にエラーになる。
調べて色々実行しましたが、わからんくて何かヒントでもいいのでお願い致します。
発生している問題・エラーメッセージ
ターミナルエラー
Post アウトプット投稿機能 投稿がうまくいくとき titleとbody,impressionが存在すれば登録できる (FAILED - 1) 投稿がうまくいかないとき titleが空だと保存できないこと (FAILED - 2) bodyが空だと保存できないこと (FAILED - 3) impressionが空だと保存できないこと (FAILED - 4) Failures: 1) Post アウトプット投稿機能 投稿がうまくいくとき titleとbody,impressionが存在すれば登録できる Failure/Error: expect(@post).to be_valid NoMethodError: undefined method `valid?' for nil:NilClass # ./spec/models/post_spec.rb:11:in `block (4 levels) in <top (required)>' 2) Post アウトプット投稿機能 投稿がうまくいかないとき titleが空だと保存できないこと Failure/Error: @post.title = nil NoMethodError: undefined method `title=' for nil:NilClass # ./spec/models/post_spec.rb:17:in `block (4 levels) in <top (required)>' 3) Post アウトプット投稿機能 投稿がうまくいかないとき bodyが空だと保存できないこと Failure/Error: @post.body = nil NoMethodError: undefined method `body=' for nil:NilClass # ./spec/models/post_spec.rb:23:in `block (4 levels) in <top (required)>' 4) Post アウトプット投稿機能 投稿がうまくいかないとき impressionが空だと保存できないこと Failure/Error: @post.impression = nil NoMethodError: undefined method `impression=' for nil:NilClass # ./spec/models/post_spec.rb:29:in `block (4 levels) in <top (required)>' Finished in 0.13419 seconds (files took 1.53 seconds to load) 4 examples, 4 failures
該当のソースコード
posts_controller.rb
posts_controller.rb
1class PostsController < ApplicationController 2 before_action :set_post,only: [:show, :edit, :update, :destroy] 3 before_action :authenticate_user!, expect: [:index] 4 5 def index 6 @posts = Post.includes(:user) 7 end 8 9 def show 10 end 11 12 def new 13 @post = Post.new 14 end 15 16 def create 17 @post = Post.new(post_params) 18 @post.user_id = current_user.id 19 if @post.valid? 20 @post.save 21 return redirect_to post_path(@post), notice: "アウトプットを投稿しました。" 22 else 23 render :new 24 end 25 end 26 27 def edit 28 if @post.user != current_user 29 redirect_to posts_path, alert: '不正なアクセスです。' 30 end 31 end 32 33 def update 34 if @post.update(post_params) 35 redirect_to posts_path(@post), notice: "アウトプットを更新しました。" 36 else 37 render :edit 38 end 39 end 40 41 def destroy 42 @post.destroy 43 redirect_to posts_path, notice: "アウトプットを削除しました。" 44 end 45 46 private 47 48 def post_params 49 params.require(:post).permit(:title, :body, :impression, :post) 50 end 51 52 def set_post 53 @post = Post.find(params[:id]) 54 end 55end 56
app/model/post.rb
class Post < ApplicationRecord belongs_to :user, optional: true with_options presence: true do validates :title validates :body validates :impression end end
spec/factories/posts.rb
FactoryBot.define do factory :post do title {"a"} body {"aa"} impression {"aaa"} association :user end end
app/views/posts/new.html.erb
<section class="hero is-dark"> <div class="hero-body"> <div class="container"> <h1 class="title"> 新規アウトプット作成 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-6"> <%= form_for @post do |f|%> <div class="field"> <%= f.label :title, "本のタイトル", class: "label" %> <%= f.text_field :title, class: "input" %> </div> <div class="field"> <%= f.label :body, "アクションプラン",class: "label" %> <%= f.text_area :body, class: "textarea" %> </div> <div class="field"> <%= f.label :impression, "感想", class: "label" %> <%= f.text_area :impression, class: "textarea" %> </div> <%= f.submit "投稿", class: "button is-success" %> <% end %> </div> </div> </div> </section>
spec/models/post_spec.rb
require 'rails_helper' RSpec.describe Post, type: :model do before do post = FactoryBot.create(:post) end describe 'アウトプット投稿機能' do context '投稿がうまくいくとき' do it "titleとbody,impressionが存在すれば登録できる" do expect(@post).to be_valid end end context '投稿がうまくいかないとき' do it 'titleが空だと保存できないこと' do @post.title = nil @post.valid? expect(@post.errors.full_messages).to include("Title can't be blank") end it 'bodyが空だと保存できないこと' do @post.body = nil @post.valid? expect(@post.errors.full_messages).to include("Body can't be blank") end it 'impressionが空だと保存できないこと' do @post.impression = nil @post.valid? expect(@post.errors.full_messages).to include("Impression can't be blank") end end end end
試したこと
コントローラーにてvalid追記
調べた結果modelでバリデーションがかかってないと思ったので、確認
app/models/post.rb
belongs_to :user
↓変更
belongs_to :user, optional: true
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/19 09:42