まずはこの通りインストールとサンプル実行
http://rspec.info/
$ sudo gem install rspec
プロジェクト/spec/bowling_spec.rb
require 'bowling' describe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should eq(0) end end
プロジェクト/spec/bowling.rb
class Bowling def hit(pins) end def score 0 end end
テストを実行
$ rspec bowling_spec.rb --format nested Bowling#score returns 0 for all gutter game Finished in 0.00049 seconds 1 example, 0 failures seiki-no-MacBook-Pro% rspec bowling_spec.rb . Finished in 0.0004 seconds 1 example, 0 failures
ここにもう少し詳しく書いてあるのでこっちの例も実行
http://rubydoc.info/gems/rspec-expectations/frames
# 配列に1が含まれているか [1,2,3].should include(1) # 配列に1と2が含まれているか複数指定 [1,2,3].should include(1, 2) # 配列の最初が1か [1,2,3].should start_with(1) # 配列の最初と次が1, 2か [1,2,3].should start_with(1,2) # 配列の最後が3か [1,2,3].should end_with(3) # 配列の最後が2, 3か [1,2,3].should end_with(2,3) {:a => 'b'}.should include(:a => 'b') # 文字列にis strが含まれているか "this string".should include("is str") #文字列がthisから始まっているか "this string".should start_with("this") # 文字列がringで終わっているか "this string".should end_with("ring")
次にRailsのテスト
参考はこちら
http://ginpen.com/2012/02/14/rspec-rails/
$ rake spec .............................. Finished in 0.6537 seconds 30 examples, 0 failures Randomized with seed 10553
ここまで実施して、また別の日に実施すると…
$ rake spec Could not find cocaine-0.4.2 in any of the sources Run `bundle install` to install missing gems.
エラー発生。。。
前は動いてたのになぜ??
$ which ruby /usr/bin/ruby
ruby1.9.3をデフォルトに指定していなかったのが原因でした。
rvm use ruby-1.9.3-head --default
として再度
$ rake spec
解決。焦りました。