&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の概要についてしか説明できませんでしたが、今回はRspecの実際の使い方について勉強していきます。
 
 -第一回はこちら
 [[Ruby on Rails/第12回勉強会 - Rspec概要]]
 
 #blikimore
 
 
 **目次 [#wd9223a3]
 #contents
 
 **日時 [#xf19520d]
 - 2007/10/19(金)
 
 **参加者 [#b239fcd8]
 -[[志田]]さん
 -[[竹村]]さん
 -[[進地]]さん
 -[[小沼]]
 
 ** 動画 [#mba5fc42]
 http://www.screencast.com/t/dq7IJeeo6
 
 *本編 [#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文|
 -上記の対応を実際のサンプルで確認する。
  $emacs test/rspec/array_spec.rb  #specファイル
  ----
  describe Array, "when empty" do #Behavior
    before do
      @empty_array = []
    end
  
    it "should be empty" do #Example
      @empty_array.should be_empty #Expectation
    end
  
    it "should size 0" do #Example
      @empty_array.size.should == 0 #Expectation
    end
  
    after do
      @empty_array = nil
    end
  end
 -上記の各要素の関連図
 --spec(1)-(n)Behavior(1)-(n)Example(1)-(n)Expectation
 -スペックファイルとテスト対象のプログラムの関連
 --1 つのクラス (モジュール) に対して必ず 1 つのスペックファイルというルールはない
 --一つのクラスに対して複数のスペックファイルで振舞を記述してもよい。
 
 ***Behavior [#wc0facb9]
 -振舞
 -Test::Unit におけるフィクスチャ (テストクラス) に相当する
 -複数のExampleを含む
 -以下のような書式
  describe Array "when empty" do #Arrayクラスがemptyのときの振舞
    ..
  end
 
 ***Example [#p3aadf0b]
 -実行可能なサンプル
 -Test::Unit におけるテストメソッドに相当する
 -複数のExpectationを含む
 -以下のような書式
   it "should be empty" do #空であること
     ...
   end
 -Exampleとは違うが、テストユニットにおけるset_up / tera_downに相当するbefore / after というメソッドがRspecにはある。
  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)|「(actual - expected).abs < delta」が成立することを期待します|
 |change|actual が Proc である場合に利用できます。change の引数を使って、期待するオブジェクト状態の変化を記述します。後ほど説明しますが、RDocの記述も参照してください|
 |eql(expected)||
 |equal(expected)||
 |have(number).items|要素数が number であることを期待します|
 |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
 
 **まとめ [#n529f97b]
 -インストールの仕方が分かった!
 -記述の仕方が分かった!
 -利便性はまだよく分からない!
 
 #blikifooter(小沼)
 tag: [[Ruby on Rails>tag/Ruby on Rails]], [[勉強会>tag/勉強会]],[[テスト>tag/テスト]]
 
 

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

アークウェブのサービスやソリューションはこちら