履歴: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 13 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2014/6/2 9:53:59 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 最新版 | 39,445 | +1 | -1 | 表示 | 差分 | — |
| 2014/6/2 9:53:59 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 39,451 | +6 | -1 | 表示 | 差分 | 選択 |
| 2014/5/30 14:33:33 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 39,363 | +2 | -0 | 表示 | 表示中 | 選択 |
| 2014/5/30 14:32:24 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 39,288 | +116 | -17 | 表示 | 差分 | 選択 |
| 2014/5/30 7:58:45 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 36,014 | +229 | -0 | 表示 | 差分 | 基準 |
| 2014/5/26 4:31:48 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 29,846 | +11 | -4 | 表示 | 差分 | 選択 |
| 2014/5/26 4:31:48 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 29,608 | +30 | -0 | 表示 | 差分 | 選択 |
| 2014/5/24 1:25:48 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 28,564 | +1 | -1 | 表示 | 差分 | 選択 |
| 2014/5/23 17:42:25 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 27,825 | +163 | -0 | 表示 | 差分 | 選択 |
| 2014/5/23 13:25:19 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 21,221 | +174 | -2 | 表示 | 差分 | 選択 |
| 2014/5/23 3:13:32 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 15,654 | +253 | -88 | 表示 | 差分 | 選択 |
| 2014/5/22 14:25:52 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 12,435 | +167 | -47 | 表示 | 差分 | 選択 |
| 2014/5/22 13:28:26 | ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト | 9,057 | — | — | 表示 | — | 選択 |
基準: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト (2014/5/30 7:58:45) → 比較: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト (2014/5/30 14:33:33)
@@ -9,6 +9,9 @@
これらを使って Rails アプリケーションをテストするためのノウハウをメモとして残したい
+ネットで調べると rspec + shoulda-matchers の記事が多くて、
+MiniTest + shoulda-context + shoulda-matchers の情報になかなかたどり着けないので。。。
+
* 環境 [#ab825549]
現在のこちらの環境は以下の通りです。
@@ -532,11 +535,11 @@
end
end
-** should redirect_to_path [#r6fcffc4]
+** should redirect_to_path と set_the_flash.to の拡張 [#r6fcffc4]
test/shoulda_macros/action_controller_macros.rb
LANG:ruby(linenumber)
- class ActionController::TestCase
+ module Shoulda::Matchers::ActionController # :nodoc:
# shoulda の redirect_to では、
# should redirect_to account_path
@@ -577,6 +580,28 @@
end
end
+ # set_session.to と同様に、
+ # set_the_flash.to{ some_code } の形の呼び出しを可能にする
+ # controller 側の I18n.locale を使ってメッセージを評価するには
+ # set_the_flash.to{ I18n.t :message_id } の形での呼び出しにが必須になる
+ class SetTheFlashMatcher
+
+ alias :to_without_block :to
+ def to(value=//, &block)
+ @value_block = block
+ to_without_block(value)
+ end
+
+ alias :matches_q_without_block :"matches?"
+ def matches?(controller)
+ if @value_block
+ to_without_block( @context.instance_eval(&@value_block) )
+ end
+ matches_q_without_block(controller)
+ end
+
+ end
+
end
*** context_for_all_locales [#r1d0c88f]
@@ -612,20 +637,20 @@
https://github.com/seattlerb/minitest
capture_exceptions で self.failures にエラーを溜めて、~
-https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L202
+https://github.com/seattlerb/minitest/blob/d5d43cef9a3fd4a0eea972dde125ed5ba1ddb821/lib/minitest/test.rb#L202
to_s で文字に直す?~
-https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L261
+https://github.com/seattlerb/minitest/blob/d5d43cef9a3fd4a0eea972dde125ed5ba1ddb821/lib/minitest/test.rb#L261
場所を表わす部分は location で、self.failure というのがどこから来るのか分からないものの、
アサート例外の location から得ているように見える~
-https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L224
+https://github.com/seattlerb/minitest/blob/d5d43cef9a3fd4a0eea972dde125ed5ba1ddb821/lib/minitest/test.rb#L224
failure は Runnable で定義されていた → 何のことはない failures.first のことだった~
-https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L345
+https://github.com/seattlerb/minitest/blob/d5d43cef9a3fd4a0eea972dde125ed5ba1ddb821/lib/minitest.rb#L345
Minitest::Assertion 例外の location はここ~
-https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L633
+https://github.com/seattlerb/minitest/blob/d5d43cef9a3fd4a0eea972dde125ed5ba1ddb821/lib/minitest.rb#L633
LANG:ruby
module Minitest
@@ -693,7 +718,7 @@
バックトレースに TestCase の行が含まれていない???
-https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/context.rb#L356
+https://github.com/thoughtbot/shoulda-context/blob/5305f310d432992e2a1e74f542c62104272edacf/lib/shoulda/context/context.rb#L356
LANG:ruby
def should_not(matcher)
@@ -742,6 +767,12 @@
add_caller_line_number(self.shoulds.last)
end
+ alias :should_eventually_without_caller_information :should_eventually
+ def should_eventually(name, &blk)
+ should_eventually_without_caller_information(name, &blk)
+ add_caller_line_number(self.should_eventuallys.last)
+ end
+
end
end
end
@@ -762,16 +793,20 @@
** minitest-focus の代わりに shoulda-focus [#g183283d]
-minitest-focus は、
-編集中のファイルの中で、ある特定のテストだけを実行したいときに
-そのテストに focus というのを付ければ良い、という便利な gem なのですが、
-shoulda-context との組み合わせはどうもよくありません。
+Minitest::Unit によるテストを便利にするための gem として minitest-focus というものがあります。
-- http://docs.seattlerb.org/minitest-focus/
-- https://github.com/seattlerb/minitest-focus
+http://docs.seattlerb.org/minitest-focus/
+これは、 編集中のテストケースの中で、ある特定のテストだけを実行したいときに そのテストに focus というキーワードを付けることで、同じテストケースの中の他のテストをすべてスキップできる、という便利な gem です。
+
+特に guard-minitest でテストを自動実行しているときには非常に便利です。
+
+ただ、shoulda-context との組み合わせはどうもよくありません。
+
そこで、shoulda-context を併用する際に使える focus を以下のように作って使っています。
+この記事は Quiita にも載せました。http://qiita.com/osamu_takeuchi/items/256e044a3e802a5abff0
+
test/shoulda_macros/shoulda-focus.rb
LANG:ruby(linenumber)
# shoulda-focus.rb
@@ -782,6 +817,9 @@
# allows testing only the focused tests,
# skipping all other unfocused ones in a TestCase.
#
+ # REVISIONS:
+ # 2014-05-30 report number of skipped tests in the result
+ #
# EXAMPLE:
#
# class MyTest < ActionController::TestCase
@@ -834,8 +872,10 @@
# call this method to mark the next context/should to be focused
def focus
unless Shoulda::Context.focus_used
- puts "\n#### FOCUS CALLED FOR [#{self.name}] WE WILL SKIP UNFOCUSED TESTS"
- puts caller(2).first,"\n"
+ puts "",
+ " * FOCUS CALLED FOR [#{self.name}] WE WILL SKIP UNFOCUSED TESTS",
+ " " + caller(2).first.sub(/:in .*/, ''),
+ ""
end
Shoulda::Context.focus_used = true
Shoulda::Context.focus_next = true
@@ -880,11 +920,31 @@
def create_test_from_should_hash(should)
if not Shoulda::Context.focus_used or should[:focused]
create_test_from_should_hash_without_focus(should)
+ else
+ else
+ # to report skipped tests verbosely, use skip() instead (will be too noisy and slow)
+ # should[:block] = Proc.new(){ skip() }
+ should[:block] = Proc.new(){ increase_skip_count_quietly() }
+ create_test_from_should_hash_without_focus(should)
end
end
end
end
end
+
+ MiniTest::Unit::TestCase.class_eval do
+ alias :run_without_quiet_skip :run
+ def run(runner)
+ @runner = runner
+ run_without_quiet_skip(runner)
+ end
+
+ def increase_skip_count_quietly()
+ @runner.instance_eval do
+ @skips += 1
+ end
+ end
+ end
* assert_XXX [#h14dab47]
@@ -1052,6 +1112,7 @@
-- set_the_flash
-- set_the_flash.to('value')
-- set_the_flash.to(/regexp/)
+-- set_the_flash.to{ calc_expectation_in_string_or_regexp } # 上のコードによる拡張
-- set_the_flash[:key]
-- set_the_flash[:key].to('value')
-- set_the_flash[:key].to(/regexp/)
@@ -1133,10 +1194,12 @@
例えば app/models/account.rb を編集すると test/models/account_test.rb が実行されるだけで、Account モデルを使う app/controllers/account_controller.rb のテストまで自動実行されるわけではありません。
-実際のところ、account_controller_test.rb を実行するように設定することまでなら簡単にできそうですが、依存関係をたどってその他のコントローラや統合テストまで起動するのは恐らく簡単じゃないのだと思います。
+実際のところ、account_controller_test.rb も合わせて実行するように設定することまでなら簡単にできそうですが、ちゃんと依存関係をたどってその他のコントローラや統合テストまで起動するのは恐らく簡単にはできないのだと思います。
ですから、app/models/account.rb の編集が一段落したら、例えば application_controller.rb に touch して全テストを実行する(そのように設定しておいて)といった使い方になるんだと思います。
+guard で全テストを実行するには、"guard(main)>" のプロンプトで何も入力せず ENTER キーを押せばよいので、そういう用途にも楽ちんに対応できますね。
+
** 表示を見やすくする [#gbbca72e]
[[minitest-reporters>https://github.com/kern/minitest-reporters]] を使うと結果をカラー表示にできるようです。
@@ -1164,6 +1227,43 @@
ここには載せませんが、確かに出力がカラーになりました。
+* should_eventually と skip() [#m30d873b]
+
+shoulda-context の機能に should_eventually というのがあって、
+通常 should と書くところを should_eventually と書いておく、
+あるいは、should に名前だけ付けて do end を与えないことで、
+ビヘイビャーを記述しつつ、実装を後から行うようにできます。
+
+ LANG:ruby
+ should_eventually "be implemented in the future" do # should_eventually
+ assert false # will not be tested for now
+ end
+
+ should "be implemented in the future, too" # should without body
+
+このときテスト画面には、
+
+ LANG:console
+ * DEFERRED: should be implemented in the future (at line 139).
+ * DEFERRED: should be implemented in the future, too (at line 143).
+
+というような表示を残すのみで、テストとしてはカウントされません。
+
+一方、MiniTest::Unit::TestCase には skip() という関数が用意されていて、
+テスト関数から skip() を呼ぶことで MiniTest::Skip という例外を投げることができて、
+そのテスト関数の以降のテストをキャンセルできます。このとき、テストは skipped としてカウントされます。
+
+ LANG:ruby
+ should "be implemented in the future" do
+ skip() # cancel testing
+ assert false # this assertion will not be tested for now
+ end
+
+当分実装しなさそうな、しばらくの間完全に忘れていて良い仕様については
+should_eventually または do end なしの should で記述しておけばよく、
+
+逆にペンディングになっている仕様があることを目立たせたければ skip() を使えば良いのだと思います。
+
* RSpec の方が良いんでしょうか [#m55d50d7]
Web 上で情報を探していると、RSpec の記事の方がたくさん目にとまります。
@@ -1181,3 +1281,4 @@
* コメント [#yfa709ad]
#article_kcaptcha
+