LoginSignup
1
2

More than 3 years have passed since last update.

RSpecの導入

Last updated at Posted at 2020-06-18

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]

  # ----- 以下追加 -----
  gem 'factory_bot_rails'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 4.0.0.beta'
end

以下のコマンドでRailsアプリに、rspecを導入できる。

bundle exec rails generate rspec:install

導入しようとしたらそもそもアプリ作り立てなので「bundle install」をしてなかった。笑

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/............
(略)
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.

再び導入↓

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

もろもろの設定

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.

    # ------------以下追加--------------
    config.time_zone = 'Tokyo'

    config.generators do |g|
      g.test_framework :rspec,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false
    end

    # 認証トークンをremoteフォームに埋め込む
    config.action_view.embed_authenticity_token_in_remote_forms = true
    # ------------ここまで--------------
end

spec/rails_helper.rb
(略)
# ----------↓コメントアウトを外した-------------
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
(略)
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

  (略)

  #-----------以下追加-------------
  config.include FactoryBot::Syntax::Methods
end

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

RSpecテストの作成

RSpecテストの実行

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2