[RUBY] I got an error "undefined method` create'" in RSpec

Everyday Rails – Getting Started with Rails Testing with RSpec for me when I was learning to test RSpec on a controller I learned something new, so I would like to output it.

problem

When I was studying about RSpec today,

reviews_spec.rb


 review = FactoryBot.create(:review)

The description

reviews_spec.rb


  review = create(:review)

I learned that it can be abbreviated, so I tried it myself.

However, after executing "bundle exec rspec", I got the following error.

Terminal


Failure/Error: review = create(:review)
     
     NoMethodError:
       undefined method `create'for …… (Omitted below)

"I wonder if the setup didn't go well ..." I thought, so please refer to Junichi Ito's blog ( link here ) I was reviewing the Rails setup and doing other research.

solution

As a result, I found out that rails_helper needed the following description. (<a href="https://qiita.com/jonakp/items/0f70eece4fe7980f81a6" "target="_blank" rel="noopener noreferrer"> Click here for reference articles )

rails_helper.rb


 #Please write at the bottom

 RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
  end

It seems that an error like this one was issued because this description was omitted.

This was at the beginning

reviews_spec.rb


 review = FactoryBot.create(:review)

The description

reviews_spec.rb


  review = create(:review)

I was able to make it an abbreviated system.

This time it was the create method, but it is the same when you want to write an abbreviated system with the build method.

Recommended Posts

I got an error "undefined method` create'" in RSpec
RSpec error resolution undefined method `feature'for RSpec: Module
[Mockito] 3.2.4 → 3.3.x, but I got an error (Iketenai)
I got an error when I ran rake routes.
rspec Error: ActionView :: Template :: Error: undefined method `name'for nil: NilClass
When I bcrypt with node + docker, I got an error
I got stuck in File
I got an error when using nextInt, nextLine and substring.
I got an error! * There is no interactive request template
[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..
I sent an email in Java
Undefined local variable or method error in partial template collection option
I got an error and couldn't push! !! [error: failed to push some refs to]
I got a Permission Denied error when I put Laravel in Docker
I made an annotation in Java.
I don't see an error in Rails bundle install ... the solution
[Ruby] undefined method `dark?'occurs in rqr_code
I want to display an error message when registering in the database
Create an HTML/XML generator in Swift (dynamicMemberLookup)
RSpec error resolution undefined method'name' in'user' factory
What I got into @Transactional in Spring
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
I got an "ActionView :: Template :: Error: Permission denied" error in the test of 3.3.1 of Rails tutorial 6th edition, so I solved it.
Nuxt.js × Create an application in Rails API mode
Where I got stuck in today's "rails tutorial" (2020/10/08)
I want to send an email in Java.
Where I got stuck in today's "rails tutorial" (2020/10/05)
Arbitrary search method in an array using binary search
Where I got stuck in today's "rails tutorial" (2020/10/06)
Where I got stuck in today's "rails tutorial" (2020/10/04)
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
Rails error undefined method ʻimage_name'for nil: NilClass handling
Check method call arguments in blocks with RSpec
Where I got stuck in today's "rails tutorial" (2020/10/07)
A story that got stuck with an error during migration in docker PHP laravel
I have a question. I get an error when playing a video in Listview on android.
I got an error when trying to install sbt to build a Scala development environment
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[JUnit] I suddenly got an initialization Error in the JUnit test that worked normally until the day before; background and solution memorandum