Home>

I am creating a simple output app.
An error occurs when executing the post (post function) test.
I researched and executed various things, but I don't understand and can give me some hints.

Terminal error

Post
  Output posting function
    When the post goes well
      Can be registered if title, body, and impression exist (FAILED -1)
    When posting goes wrong
      Cannot save if title is empty (FAILED-2)
      Cannot save if body is empty (FAILED -3)
      Cannot save if impression is empty (FAILED -4)
Failures:
  1) Post output Post function When posting is successful, you can register if title, body, and impression exist.
     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 output Post function When a post does not go well If the title is empty, it cannot be saved.
     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 output Post function When posting is not successful, it cannot be saved if the body is empty.
     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 output Post function When posting is not successful, it cannot be saved if the impression is empty.
     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
Corresponding source code

posts_controller.rb

class PostsController</pre>
<p>app/model/post.rb</p>
<pre><code>class Post</pre>
<p>spec/factories/posts.rb</p>
<pre><code>FactoryBot.define do
  factory: post do
    title {"a"}
    body {"aa"}
    impression {"aaa"}
    association: user
  end
end

app/views/posts/new.html.erb

<section>                  Create new output
          </section><section>                <% = form_for @post do | f |%>        <% = f.label: title, "book title", class: "label"%>          <% = f.text_field: title, class: "input"%>                                  <% = f.label: body, "action plan", class: "label"%>          <% = f.text_area: body, class: "textarea"%>                                  <% = f.label: impression, "impression", class: "label"%>          <% = f.text_area: impression, class: "textarea"%>                    <% = f.submit "submit", class: "button is-success"%>     <% end%>        </section>

spec/models/post_spec.rb

require'rails_helper'
RSpec.describe Post, type :: model do
  before do
    post = FactoryBot.create (: post)
  end
  describe'output posting function' do
    context'when the post goes well' do
      it "Can be registered if title, body and impression exist" do
        expect (@post) .to be_valid
      end
    end
    context'when the post goes wrong' do
      it'Cannot save if title is empty' do
        @ post.title = nil
        @ post.valid?
        expect (@ post.errors.full_messages) .to include ("Title can't be blank")
      end
      it'Cannot save if body is empty' do
        @ post.body = nil
        @ post.valid?
        expect (@ post.errors.full_messages) .to include ("Body can't be blank")
      end
      it'Cannot save if impression is empty' do
        @ post.impression = nil
        @ post.valid?
        expect (@ post.errors.full_messages) .to include ("Impression can't be blank")
      end
    end
  end
end
What I tried

Added valid on the controller
As a result of checking, I thought that the model was not validated, so I confirmed it
app/models/post.rb

belongs_to: user


↓ Change

belongs_to: user, optional: true
Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    NoMethodError: undefined method `valid?'For nil: NilClass

    You said that nil variables don't have a method called `valid?'.
    Let's check the contents of the variable where this guy is stuck

Related articles