[RUBY] Introduction de RSpec

Installer 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]

  # -----Ajouté ci-dessous-----
  gem 'factory_bot_rails'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 4.0.0.beta'
end

Vous pouvez installer rspec dans votre application Rails avec la commande suivante.

bundle exec rails generate rspec:install

Lorsque j'ai essayé de l'installer, je n'ai pas fait d '"installation groupée" car c'était une nouvelle application. 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/............
(Omis)
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.

Introduit à nouveau ↓

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

Divers réglages

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.

    # ------------Ajouté ci-dessous--------------
    config.time_zone = 'Tokyo'

    config.generators do |g|
      g.test_framework :rspec,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false
    end
    
    #Incorporer le jeton d'authentification sous forme distante
    config.action_view.embed_authenticity_token_in_remote_forms = true
    # ------------Jusque là--------------
end

spec/rails_helper.rb


(Omis)
# ----------↓ j'ai décommenté-------------
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
(Omis)
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

(Omis)

  #-----------Ajouté ci-dessous-------------
  config.include FactoryBot::Syntax::Methods
end

Créer 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

Création d'un test RSpec

Exécuter le test RSpec

référence

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

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

Recommended Posts

Introduction de RSpec
Présentation de la devise
Installation de RSpec
Configuration RSpec
Rspec, TDD (1)
Présentation de la devise
Bonjour RSpec
Présentation de la bibliothèque
Présentation de TDD (succès)
Rspec introduction note_Rails
Présentation de Rspec avec Ruby on Rails x Docker
[Rails] Présentation de jquery
[Rails] Présentation du dispositif
Présentation de Rspec, un framework de test pour Ruby on Rails
Présentation d'ActionView :: Component
Présentation de TDD (échec)
Principes de base de Rspec [Rails]
[Rails5] Rspec -validation-