&tag(RSpec);
*目次 [#h9e8cd00]
#contents

*関連ページ [#uc148ed0]
-[[./expect]]
-[[./Rails3へ導入]]
-[[./Rails4へ導入]]
-[[Rails3+Guard+Spork+RSpec]]
-[[Rails4+Guard+Spring+RSpec]]
-[[RSpec3]]

*参考情報 [#n67a647c]
-[[Rails]]
-[[WebMock]]
-[[database_cleaner]]
-[[factory_girl]]
-[[Rubyist Magazine - スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編):http://jp.rubyist.net/magazine/?0021-Rspec]]
-[[RSpecの標準Matcher一覧表 - 本当は怖い情報科学:http://d.hatena.ne.jp/keisukefukuda/20080124/p1]]
-[[RSpecでテストコードを書いたまとめ - (゚∀゚)o彡 sasata299's blog:http://blog.livedoor.jp/sasata299/archives/51277861.html]]
-[[Rails3+Guard+Spork+RSpec]]
-[[A Guide to Testing Rails Applications ― Ruby on Rails Guides:http://guides.rubyonrails.org/testing.html]] RSpecじゃなくて、minitestを使いたい場合
*RSpecとは何か [#g48e3d61]
-プログラムの振る舞いを記述するためのドメイン特化言語
#pre{{
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
}}

*使用方法(簡単編) [#n3877647]
-rspecをインストール
-array_spec.rbを作る。
#pre{{
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
}}
-実行。-cオプションをつけると色を表示できる(rspec -c ...)
#pre{{
$ rspec array_spec.rb
rspec array_spec.rb
..

Finished in 0.00312 seconds
2 examples, 0 failures
}}

*マッチャー一覧 [#m9de2191]
-xxx.should マッチャー / yyy.should_not マッチャーのように使用する(==もマッチャー)。
-[[RSpecの標準Matcher一覧表 - 本当は怖い情報科学:http://d.hatena.ne.jp/keisukefukuda/20080124/p1]]


*Tips [#q700f196]
**データファイルを読み込む [#u032113d]
-[[ruby on rails - relative File Path in RSpec - Stack Overflow:http://stackoverflow.com/questions/9345412/relative-file-path-in-rspec]]にあるように、__FILE__を使うしかない?
*トラブルシューティング [#ob7d9adf]
**spec実行時に「`require': cannot load such file -- spec_helper (LoadError)」 [#t6653edb]
-spec_helperを作ってないのかもしれない。以下のコマンドを実行してみる。
 rails generate rspec:install
**describeでエラー発生「syntax error, unexpected keyword_do_block, expecting => (SyntaxError)」 [#h626e9d5]
-describeの書き方が変わったらしい。[[Myron Marston &#187; RSpec 2.99 and 3.0 RC1 have been released!:http://myronmars.to/n/dev-blog/2014/05/rspec-2-99-and-3-0-rc-1-have-been-released]]によると、「Issue a deprecation when described_class is accessed from within a nested describe <SomeClass> example group, since described_class will return the innermost described class in RSpec 3 rather than the outermost described class, as it behaved in RSpec 2. (Myron Marston)」とある。
--以前: 
 describe Simple, :type => :model ,'説明分' do
--以後:
 describe '説明分', :type => :model  do


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