LoginSignup
1
1

More than 3 years have passed since last update.

Ruby最新版の挙動をRSpec+Guardで試した時の環境構築メモ

Last updated at Posted at 2019-05-23

はじめに

Rubyのバージョンが上がったときなどにサクッと挙動を確かめられるよう書いた自分用のメモです
(ちなみにbrewやRubyをインストールする時間を除けば3分で構築できました)

今回利用したツールの各バージョン

  • macOS 10.13.6
  • brew 2.1.3
  • rbenv 1.1.2

最新Rubyインストール

インストールできるRubyのバージョンリストを最新にする

brew update && brew upgrade ruby-build

インストール可能な一覧を見てバージョンを決める

rbenv install -l
...
2.7.0-dev
...

指定したバージョンのインストール

rbenv install 2.7.0-dev

作業場所用意

ここで「sample」という作業ディレクトリを作り
以下の作業はこの中で行います

mkdir ~/sample
cd ~/sample

インストールしたバージョンに切り替える

rbenv local 2.7.0-dev
ruby -v #=> ruby 2.7.0dev (2019-05-22 trunk ca435ed04a) [x86_64-darwin17]

RSpec + Guardインストール

まずはbundlerインストール

gem install bundler
bundler -v #=> Bundler version 2.1.0.pre.1

Gemfile新規作成

bundle init # Gemfileが生成される

Gemfileに必要なgem追記

Gemfile
# 以下追記
gem "rspec"
gem "guard"
gem "guard-rspec"

gemインストール

.bundle/ にインストールすることにします

bundle config set path .bundle # ちなみに --path .bundleは非推奨になるらしい
bundle install

今回は以下のバージョンがインストールされました

  • rspec (3.8.0)
  • guard (2.15.0)
  • guard-rspec (4.7.3)

RSpec準備

bundle exec rspec --init

Guard準備

bundle exec guard init

テストコード作成

> spec/my_spec.rb
spec/my_spec.rb
require 'spec_helper'

RSpec.describe 'MySample' do
  describe 'methodメソッド' do
    subject { 123.method(:to_s) }
    it { expect(subject.class).to eq(Method) }
    it { expect(subject.call).to eq('123') }
    it { expect(subject.()).to eq('123') }
  end

  describe 'メソッド参照演算子' do
    subject { 123.:to_s }
    it { expect(subject.class).to eq(Method) }
  end
end

Guard起動

bundle exec guard

とりあえずenter押せば全テストケースを実行してくれます。
あとはlib/my_spec.rbを保存するたびにテストコードが走るはずです。

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