LoginSignup
0
0

More than 3 years have passed since last update.

【第1章】 RailsチュートリアルにRSpecを導入してみた

Posted at

はじめに

こんにちは、ご覧いただきありがとうございます。エンジニアとして駆け出したい羊です。
Railsチュートリアル(第6版)にRSpec(とRuboCop)を導入して学習をしてみようと思い、学習帳代わりに記していこうと思います。
Railsチュートリアルは2週目なので細かいところは飛ばして手早く進めます。
間違いがあれば是非とも優しくご指摘いただけますと幸いです。

第1章 ゼロからデプロイまで

Rails new

$ rails new _6.0.3_ rails-tutorial

Gitのセットアップ

初回のセットアップ

$ git config --global user.name "自分の名前"
$ git config --global user.email your.email@example.com

エイリアス設定

$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.br branch
$ git config --global alias.co checkout

パスワードの保持

$ git config --global credential.helper "cache --timeout=86400"

初回のリポジトリセットアップ

$ git init
Reinitialized existing Git repository in /home/vagrant/work/rails_tutorial/.git/

$ git add -A
$ git status
$ git ci -m "Initialize repository"
$ git log

GitHubをリモートoriginに追加してそのリポジトリにpushする

$ git remote add origin https://github.com/moutoon/rails_tutorial.git
$ git push -u origin master

Herokuのセットアップ

Gemfileの書き換え

Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem 'rails',      '6.0.3'
gem 'puma',       '4.3.6'
gem 'sass-rails', '5.1.0'
gem 'webpacker',  '4.0.7'
gem 'turbolinks', '5.2.0'
gem 'jbuilder',   '2.9.1'
gem 'bootsnap',   '1.4.5', require: false

group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console',           '4.0.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.1.0'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'capybara',                 '3.28.0'
  gem 'selenium-webdriver',       '3.142.4'
  gem 'webdrivers',               '4.1.2'
  gem 'rails-controller-testing', '1.0.4'
  gem 'minitest',                 '5.11.3'
  gem 'minitest-reporters',       '1.3.8'
  gem 'guard',                    '2.16.2'
  gem 'guard-minitest',           '2.4.6'
end

group :production do
  gem 'pg', '1.1.4'
end

# Windows ではタイムゾーン情報用の tzinfo-data gem を含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
$ bundle install --without production
$ bundle update

Herokuにデプロイする

$ git commit -a -m "Update Gemfile for Heroku"
$ heroku login --interactive
$ heroku create
$ git push heroku master

RSpecとRuboCopの導入

RSpecの導入

gemのインストール

Gemfile
(前略)

group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
  gem "rspec-rails" # 追加
end

(後略)
$ bundle install

rspecの導入

$ bundle exec rails generate rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

セットアップ

--require spec_helper   # 呼び出すファイルの指定
--color                 # 出力結果に色を付ける
--format documentation  # 表示結果をわかりやすく表示する
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 RailsTutorial
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    config.generators do |g|
      g.test_framework :rspec,
                       fixtures: true,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false,
                       controller_specs: true,
                       request_specs: false
      g.fixture_replacement :factory_bot, dir: "spec/factories"
    end
  end
end

SpringでRSpecを高速化する

Gemfile

(前略)

group :development do
  gem 'web-console',           '4.0.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.1.0'
  gem 'spring-watcher-listen', '2.0.1'
  gem 'spring-commands-rspec'  # 追加
end

(後略)
$ bundle install
$ bundle exec spring binstub rspec

RSpecの実行

$ bin/rspec
Running via Spring preloader in process 5253
No examples found.

Finished in 0.00058 seconds (files took 0.81129 seconds to load)
0 examples, 0 failures

RuboCopの導入

gemのインストール

Gemfile

(前略)

group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
  gem "rspec-rails"
  gem 'rubocop-airbnb' # 追加
end

(後略)

セットアップ

.rubocop_airbnb.yml
require:
  - rubocop-airbnb

.rubocop.yml
inherit_from:
  - .rubocop_airbnb.yml

Rails:
  Enabled: true

# 文字数の上限を80文字から変更
LineLength:
  Max: 130

AllCops:
  Exclude:
    - 'db/**/*'
    - 'bin/*'
    - 'config/environments/*'
    - 'config/application.rb'
    - 'config/initializers/*'
    - 'config/spring.rb'
    - 'lib/tasks/*'
    - 'vendor/**/*'
    - 'path/ruby'

Style/TrailingCommaInHashLiteral:
  Enabled: false

Style/BlockDelimiters:
  Enabled: false

Layout/FirstHashElementLineBreak:
  Enabled: false

Airbnb/OptArgParameters:
  Enabled: false

Style/ColonMethodCall:
  Enabled: false

Layout/IndentationConsistency:
  Enabled: false

Layout/IndentationWidth:
  Enabled: false

Style/MixinUsage:
  Enabled: false

Style/MutableConstant:
  Enabled: false

Rubocopの実行

$ bundle exec rubocop

さいごに

これでRails、Git、Heroku、RSpec、RuboCopのセットアップが終わりました。
次回からはイシューとプルリクも加えて、疑似チーム開発を導入して第3章を進めていこうと思います。

0
0
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
0
0