- 追加された行はこの色です。
- 削除された行はこの色です。
&pgid; **このページは? [#n3541592] 下記のネタを元にRspecについて勉強してみるページです。 -Rubyist Magazine - スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編) http://jp.rubyist.net/magazine/?0021-Rspec -File: TUTORIAL.ja http://kakutani.com/trans/rspec/TUTORIAL_ja.html 今回は第2回ということで前回に続きです。 今回はRspecの実際の使い方について勉強します。 -第一回はこちら [[Ruby on Rails/第12回勉強会 - Rspec概要]] **目次 [#wd9223a3] #contents **日時 [#xf19520d] - 2007/10/19(金) **参加者 [#b239fcd8] ** 動画 [#mba5fc42] *本編 [#v60241bf] **インストールしてみる [#rd18e79b] http://jp.rubyist.net/magazine/?0021-Rspec#l18 -インストール $ su # gem install rspec Bulk updating Gem source index for: http://gems.rubyforge.org Successfully installed rspec-1.0.8 Installing ri documentation for rspec-1.0.8... Installing RDoc documentation for rspec-1.0.8... -ちゃんとインストールされたかチェック $spec -v RSpec-1.0.8 (r2338) - BDD for Ruby http://rspec.rubyforge.org/ ちゃんとインストールできた。 **とりあえず使ってみる [#kc561a22] http://jp.rubyist.net/magazine/?0021-Rspec#l19 -上記ページでサンプルとしてあるArrayクラスへの振舞の記述をしてみる。 $emacs test/rspec/array_spec.rb で以下を貼り付け describe Array, "when empty" do before do @empty_array = [] end it "should be empty" do @empty_array.should be_empty end it "should size 0" do @empty_array.size.should == 0 end after do @empty_array = nil end end -で実行 $ spec array_spec.rb .. Finished in 0.047857 seconds 2 examples, 0 failures うーん、サンプル通り。 -色をつける $ spec -c test/rspec/array_spec.rb -仕様書っぽく出力する $ spec -fs test/rspec/array_spec.rb Array when empty - should be empty - should size 0 Finished in 0.007583 seconds 2 examples, 0 failures **記述ルールについて [#a7f29185] http://jp.rubyist.net/magazine/?0021-Rspec#l26 -サンプルを掲載 ***構造 [#p97f27ff] |Rspecの構成要素|記述例|testUnitで対応するもの|h |specファイル|array_rspec.rb|Testファイル| |Behavior|describe Array, "when empty" do end|testクラス| |Example| it "should be empty" do end|testケース| |Expectation|@empty_array.should be_empty|assert文| -1 つのクラス (モジュール) に対して必ず 1 つのスペックファイルというルールはない -spec(1)-(n)Behavior(1)-(n)Example(1)-(n)Expectation ***Behavior [#wc0facb9] -振舞 -Test::Unit におけるフィクスチャ (テストクラス) に相当する -複数のExampleを含む -以下のような書式 describe Array "when empty" do #Arrayクラスがemptyのときの振舞 .. end ***Example [#p3aadf0b] -実行可能なサンプル -Test::Unit におけるテストメソッドに相当する -複数のExpectationを含む -以下のような書式 it "should be empty" do #空であること ... end -testのset_up、tera_downに相当するbefore / after というメソッドがある。 before do end after do end --before(:all),after(:all)というものもある。これはExampleのたびにではなく、Behaviourを実行する最初の一回のみ実行される。 ***Expectation [#fe5cad99] -プログラムに期待する動作や動作結果 -Test::Unit におけるassert文に相当する -以下のような書式 result.should == 37 #結果は37となる ↓一般化 actual.should matcher(expected) -matcherの部分には以下のようなものを使うことができる -マッチャ |マッチャ名|内容|h |>|~演算子のマッチャ| |<|| |<=|| |==|| |===|| |=~|| |>|| |>=|| |>|~演算子以外のビルトインのマッチャ| |be_close(expected, delta)|ほーげほげ| |change|| |eql(expected)|| |equal(expected)|| |have(number).items|| |have_at_least(number).items|| |have_at_most(number).items|| |have_exactly|| |include(expected)|actual が String か Array である場合に、actual.include?(expected) を期待します| |match(regexp)|actial =~ regexp であることを期待します| |raise_error(expected)|| |respond_to(*names)|| |satisfy{}|| |throw_symbol(:expected=nil)|| |>|~ほげマッチャ| |be_xxx|should be_empty,be_instance_of,be_kind_oflなど。(be_a_kind_ofなど冠詞をつけることもできる)| |be_true|| |be_false|| |have_xxx|| |>|~ユーザー定義のマッチャ| ***特殊なExpectation [#x3d69d9b] -vilolated violated "always fail!" # 必ず実行に失敗する -pending pending("for obvious reason") do true.should be_false # 保留したい評価(未実装など) end **テストとの比較 [#yace69dd] **まとめ [#n529f97b] -