Add rspec for Rails and factory_bot gem to Gemfile
Gemfile
group :development, :test do
  gem "rspec-rails"
  gem "factory_bot_rails"
end
Rebuild with Docker
docker-compose build
Introduce rspec to Rails app with the following command
$ bundle exec rails generate rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb
config/application.rb
config.generators do |g|
  g.test_framework :rspec, 
        #Write unnecessary test files with false as shown below
        view_specs: false, 
        helper_specs: false, 
        controller_specs: false, 
        routing_specs: false
end
rails g rspec:view Homes
However, with this alone, only the folder /spec/views/homes/ will be created.
Create  index.html.erb_spec.rb under /spec/views/homes/ and write the following
ruby:/spec/views/homes/index.html.erb_spec.rb
require 'rails_helper'
RSpec.describe "homes/index", type: :view do
  it 'should display top page' do
    visit "/"
    expect(page).to have_content 'Strained'
  end
end
The following supplement
visit "/": transition to root expect (page) .to have_content'string': Make sure there is a "string" on the current display page$ rspec
homes/index
  should display top page
Finished in 24.64 seconds (files took 7.02 seconds to load)
1 example, 0 failures
 0 failures, so the test succeeded
 visit cannot be usedspec/spec_helper.rb
...Abbreviation
require 'capybara/rspec'
RSpec.configure do |config|
  config.include Capybara::DSL #Postscript
...Abbreviation
Recommended Posts