[RUBY] Introducing RSpec

Install Gem

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]

  # -----Added below-----
  gem 'factory_bot_rails'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 4.0.0.beta'
end

You can install rspec in your Rails app with the following command.

bundle exec rails generate rspec:install

When I tried to install it, I didn't do "bundle install" because the app was created in the first place. Lol ↓

asatokensei@MacBook-Air tomalog % bundle exec rails generate rspec:install
Could not find gem 'factory_bot_rails' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
asatokensei@MacBook-Air tomalog % bundle install
Fetching gem metadata from https://rubygems.org/............
(Omitted)
Bundle complete! 18 Gemfile dependencies, 87 gems now installed.
Gems in the group production were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Introduced again ↓

asatokensei@MacBook-Air tomalog % bundle exec rails generate rspec:install
/Users/asatokensei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/asatokensei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Various settings

config/application.rb



require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Tomalog
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.

    # ------------Added below--------------
    config.time_zone = 'Tokyo'

    config.generators do |g|
      g.test_framework :rspec,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false
    end
    
    #Embed authentication token in remote form
    config.action_view.embed_authenticity_token_in_remote_forms = true
    # ------------So far--------------
end

spec/rails_helper.rb


(Omitted)
# ----------↓ I uncommented-------------
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
(Omitted)
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

(Omitted)

  #-----------Added below-------------
  config.include FactoryBot::Syntax::Methods
end

Create spec / factories / users.rb ↓

spec/factories/users.rb


FactoryBot.define do
    factory :user do
        name { 'TestUser' }
        sequence(:email) { |n| "test#{n}@example.com" }
        password { 'password' }
    end
end

Creating an RSpec test

Run RSpec test

reference

https://qiita.com/Ushinji/items/522ed01c9c14b680222c

https://qiita.com/ryouzi/items/de7336e6175530723b30

Recommended Posts

Introducing RSpec
Introducing devise
RSpec preparation
Install RSpec
RSpec Setup
RSpec Basics
Rspec, TDD (1)
Introducing devise
Hello RSpec
Introducing the library
Introducing TDD (success)
Rspec introduction memo_Rails
Introducing Rspec with Ruby on Rails x Docker
[Rails] Introducing jquery
[Rails] Introducing devise
Introducing Rspec, a Ruby on Rails test framework
Introducing ActionView :: Component
Introducing TDD (Failure)
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)