Ruby RSpec導入
インストール
この本のまとめです。 とても良いと思います。
gem install rspec
テストファイル作成
- テストファイルは filename_spec.rb と名付けるのが一般的
Rspecがあれば、他にドキュメントいらないと言われている。
Rspecのドキュメントはこちらがいいかな relishapp.com
テストファイルから tanto_spec.rb
require_relative 'tanto' describe Tanto do //インスタンス生成の書き方 let(:tanto) { Tanto.new } //example以下でテストしたいことを記載 日本語でかけるので良いと思う example '担当の給料は基本給と同じ' do //expectメソッドで、関数を呼んで期待値を記載する。 eq で等しくなるほかはドキュメント参照 expect(tanto.calculate_salary(100)).to eq 100 end end
tantoクラス shainを継承している
require_relative './shain.rb' class Tanto < Shain def standup puts "担当は慌てて起立しました" end def calculate_salary(kihonkyu) kihonkyu end end
shain クラス これをtantoではオーバーライドしている
class Shain def standup puts "社員はとりあえず起立する" end def calculate_salary(kihonkyu) kihonkyu end end
テストを自動化するのに .rspecファイルを作成
その為に rakeをインストール=>ビルドするツール
$ gem install rake //== Fetching: rake-12.3.2.gem (100%) Successfully installed rake-12.3.2 Parsing documentation for rake-12.3.2 Installing ri documentation for rake-12.3.2 Done installing documentation for rake after 0 seconds 1 gem installed
Rakefile を作成
require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = FileList['*_spec.rb'] end task default: :spec
これで
rake
だけで、_spec.rbのファイルを全て実行できる。