【RSpec】RubyのみでRSpecを用いる方法について簡単にまとめてみた【Railsなし】|TechTechMedia
この記事を参考にしました。
- directoryを作って移動する
mkdir my-project
cd my-project
- gitをスタートする
git init
- Gemfileを作成する
bundle init
- Gemfileに書き込む
gem "rspec"
- bundle installする
bundle install
- RSpecをスタートする
bundle exec rspec --init
- 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 ←削除
- libディレクトリを作る
mkdir lib
- libディレクトリの下に、Rubyのファイルを作る。
touch test.rb
- 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
- 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)
- Rubyを実行する
ruby ./lib/test.rb
- RSpecを実行する
bundle exec rspec