rspec install
[vagrant@localhost rspec]$ gem install rspec [vagrant@localhost rspec]$ ruby -v ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] [vagrant@localhost rspec]$ rspec -v 3.5.4
TDD/BDD 開発サイクル
1.失敗するテストを書く(Red)
2.最小限のコードを書いてテストをパスさせる(Green)
3.リファクタリング
initフォルダの作成
[vagrant@localhost rspec]$ rspec --init create .rspec create spec/spec_helper.rb
テストファイルは〇〇_spec.rbとし、specフォルダの中に書きます。
1.ミスするコード
RSpec.describe "A calc" do
it "given 2 and 3, return 5" do
calc = Calc.new
epect(calc.add(2, 3)).to eq(5)
end
end
[vagrant@localhost rspec]$ rspec
F
Failures:
1) A calc given 2 and 3, return 5
Failure/Error: calc = Calc.new
NameError:
uninitialized constant Calc
# ./spec/calc_spec.rb:3:in `block (2 levels) in '
Finished in 0.00317 seconds (files took 0.32741 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/calc_spec.rb:2 # A calc given 2 and 3, return 5
2.パスするコードを作成
class Calc
def add(a, b)
5 # 仮実装
end
end
require 'calc'
RSpec.describe "A calc" do
it "given 2 and 3, returns 5" do
calc = Calc.new
expect(calc.add(2, 3)).to eq(5)
end
end
実行
[vagrant@localhost rspec]$ rspec . Finished in 0.00095 seconds (files took 0.10059 seconds to load) 1 example, 0 failures