仕事中にRSpecでちょっとした発見があったので忘れないうちにブログに残しておきます
spec
describe String do it "match with be_include" do "cure peace".should be_include "azatoi" end it "match with include" do "cure peace".should include "azatoi" end end
この2つは両方とも特定の文字列が含まれるかどうかのテストですが、エラーメッセージが全く違います
be_includeのテスト結果
it "match with be_include" do "cure peace".should be_include "azatoi" end
1) String match with be_include
Failure/Error: "cure peace".should be_include "azatoi"
expected include?("azatoi") to return true, got false
includeのテスト結果
it "match with include" do "cure peace".should include "azatoi" end
2) String match with include
Failure/Error: "cure peace".should include "azatoi"
expected "cure peace" to include "azatoi"
理由
名前が似ている be_include と include ですが、中身は全く違います
be_include
RSpecでは
some_instance.should be_xxxx
のような呼び出しがあった時は
some_instance.xxxx?
という呼び出しに変わり、これを評価した結果がtrueじゃなかったらテストが失敗します。
今回の場合は String#include? が呼ばれます。
RSpecにはtrueかfalseしか入ってきていないためエラーメッセージでもtrueかfalseしかチェックしていないことが分かります。
1) String match with be_include
Failure/Error: "cure peace".should be_include "azatoi"
expected include?("azatoi") to return true, got false
Test::Unitで置き換えると
assert( "cure peace".include?("azatoi") )
みたいな感じです。
include
Spec::Matchers#include が呼ばれます。
文字列がmatcherに渡されているため、エラーメッセージにも実測値と期待値の両方が出ています。
2) String match with include
Failure/Error: "cure peace".should include "azatoi"
expected "cure peace" to include "azatoi"
結論
テストする内容が同じならエラーメッセージが分かりやすい(情報量が多い)方を選んだ方がいいです
余談
JUnitのテストで
String actual = "〜";
assertTrue( actual.contains(expected) );
なんて書いている人は今すぐJUnit実践入門を買いましょう(ステマ)
gist
The RSpec Book (Professional Ruby Series)
- 作者: David Chelimsky,Dave Astels,Zach Dennis,角谷 信太郎,豊田 祐司,株式会社クイープ
- 出版社/メーカー: 翔泳社
- 発売日: 2012/02/22
- メディア: 大型本
- 購入: 7人 クリック: 136回
- この商品を含むブログ (13件) を見る
JUnit実践入門 ~体系的に学ぶユニットテストの技法 (WEB+DB PRESS plus)
- 作者: 渡辺修司
- 出版社/メーカー: 技術評論社
- 発売日: 2012/11/21
- メディア: 単行本(ソフトカバー)
- 購入: 14人 クリック: 262回
- この商品を含むブログ (40件) を見る