Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lef237/97b85142faed2aee0d063b6933d1063f to your computer and use it in GitHub Desktop.
Save lef237/97b85142faed2aee0d063b6933d1063f to your computer and use it in GitHub Desktop.
RubyとRSpecでTDDの環境を構築する手順

RubyとRSpecでTDDの環境を構築する手順

【RSpec】RubyのみでRSpecを用いる方法について簡単にまとめてみた【Railsなし】|TechTechMedia

この記事を参考にしました。

  1. directoryを作って移動する
mkdir my-project
cd my-project
  1. gitをスタートする
git init
  1. Gemfileを作成する
bundle init
  1. Gemfileに書き込む
gem "rspec"
  1. bundle installする
bundle install
  1. RSpecをスタートする
bundle exec rspec --init
  1. spec_helper.rbを有効化する(省略可)

=begin=endのコメントアウトを削除する。

※この操作をすることでテストの実行結果が分かりやすくなる。

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin ←削除
  # This allows you to limit a spec run to individual examples or groups

# 省略

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed
=end ←削除
  1. libディレクトリを作る
mkdir lib
  1. libディレクトリの下に、Rubyのファイルを作る。
touch test.rb
  1. spec/test_spec.rbにテストを書く。
touch test_spec.rb
require_relative '../lib/test'

RSpec.describe Calculate do
  it '1000の半分は500になる' do
    calculate = Calculate.new(1000)
    expect(calculate.half_num).to eq 500
  end
end
  1. test.rbにコードを書く。
#!/usr/bin/env ruby

class Calculate
  def initialize(num)
    @num = num
  end

  def half_num
    @num / 2
  end
end

puts Calculate.new(1000)
  1. Rubyを実行する
ruby ./lib/test.rb
  1. RSpecを実行する
bundle exec rspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment