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.
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.
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