[RUBY] Einführung von RSpec

Installieren Sie 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]

  # -----Unten hinzugefügt-----
  gem 'factory_bot_rails'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 4.0.0.beta'
end

Sie können rspec mit dem folgenden Befehl in Ihrer Rails-App installieren.

bundle exec rails generate rspec:install

Als ich versuchte, es zu installieren, habe ich keine "Bundle-Installation" durchgeführt, da es sich um eine neue App handelte. 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/............
(Weggelassen)
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.

Wieder eingeführt ↓

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

Verschiedene Einstellungen

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.

    # ------------Unten hinzugefügt--------------
    config.time_zone = 'Tokyo'

    config.generators do |g|
      g.test_framework :rspec,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false
    end
    
    #Integrieren Sie das Authentifizierungstoken in Remote-Form
    config.action_view.embed_authenticity_token_in_remote_forms = true
    # ------------Bisher--------------
end

spec/rails_helper.rb


(Weggelassen)
# ----------↓ Ich habe nicht kommentiert-------------
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
(Weggelassen)
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

(Weggelassen)

  #-----------Unten hinzugefügt-------------
  config.include FactoryBot::Syntax::Methods
end

Erstellen Sie 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

Erstellen eines RSpec-Tests

Führen Sie den RSpec-Test aus

Referenz

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

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

Recommended Posts

Einführung von RSpec
Devise vorstellen
RSpec-Installation
RSpec Setup
Rspec, TDD (1)
Devise vorstellen
Hallo RSpec
Vorstellung der Bibliothek
Einführung von TDD (Erfolg)
Rspec Einführung note_Rails
Einführung in Rspec mit Ruby on Rails x Docker
[Rails] Einführung in jquery
[Rails] Einführung in das Gerät
Einführung in Rspec, ein Testframework für Ruby on Rails
Einführung in ActionView :: Component
Einführung von TDD (Failure)
Rspec Basics [Rails]
[Rails5] Rspec -validierung-