履歴: ソフトウェア/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/23 3:13:32) → 比較: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト (2014/5/23 13:25:19)
@@ -9,6 +9,13 @@
これらを使って Rails アプリケーションをテストするためのノウハウをメモとして残したい
+* 環境 [#ab825549]
+
+現在のこちらの環境は以下の通りです。
+-rails 4.0.4
+-minitest 4.7.5
+-shoulda 3.5.0
+
* いろいろ検討中 [#rc1df26c]
まだちゃんとしたテストは書き始めたばかりなので全然やり方が定まってません。
@@ -257,7 +264,6 @@
should "not create account" do
assert_equal 0, Account.count
end
- should set_the_flash # already login
should render_template :new
end
@@ -431,7 +437,7 @@
# I18n.translate の翻訳の抜けを検出する
require 'test_i18n_translation_strictly'
- # should 用のマクロを読み込む
+ # shoulda 用のマクロを読み込む
Dir[File.expand_path('../shoulda_macros', __FILE__) << '/*.rb'].each do |file|
require file
end
@@ -577,6 +583,172 @@
というのも作っておくと良さそう
+** エラーの生じた should を特定しやすくする [#wc579ebc]
+
+shoulda からのエラーメッセージが以下のように shoulda
+ライブラリや shoulda macro の中を指してしまうことがあります。
+
+ LANG:console
+ 1) Failure:
+ AccountControllerTest#test_: with locale en, without login, invalid POST #create
+ should set the flash.
+ [/var/lib/gems/2.0.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:344]:
+ Expected the flash to be set, but no flash was set
+
+このようなメッセージからエラーが生じたテストの場所を特定するのはなかなか手間がかかります。
+
+で、例外のバックトレースをいじって改善できない物か???と考えたわけです。
+
+ActiveSupport::TestCase ~
+https://github.com/rails/rails/blob/master/activesupport/lib/active_support/test_case.rb
+
+gem 'minitest' を取り込んで ::Minitest::Test を継承している。ruby 標準の minitest ではないらしい?
+
+これかな?~
+https://github.com/seattlerb/minitest
+
+capture_exceptions で self.failures にエラーを溜めて、~
+https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L202
+
+to_s で文字に直す?~
+https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L261
+
+場所を表わす部分は location で、self.failure というのがどこから来るのか分からないものの、
+アサート例外の location から得ているように見える~
+https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L224
+
+failure は Runnable で定義されていた → 何のことはない failures.first のことだった~
+https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L345
+
+Minitest::Assertion 例外の location はここ~
+https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L633
+
+ LANG:ruby
+ module Minitest
+ VERSION = "5.3.4" # :nodoc:
+ ...
+
+ class Assertion < Exception
+ ...
+
+ def location
+ last_before_assertion = ""
+ self.backtrace.reverse_each do |s|
+ break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
+ last_before_assertion = s
+ end
+ last_before_assertion.sub(/:in .*$/, "")
+ end
+
+この backtrace を遡っている部分の条件を変更してやれば良い・・・のかと思ったら違うみたいです???
+どこかで間違った???
+
+ああ、動かしてるバージョンが段違いでした orz~
+上記 seattlerb/minitest でメンテしているのは 5.x.x 系列で、4.x.x 系列は 4.7.2 あたりが最後?
+
+古いコードだと、普通に rescue で捕まえて runner.puke してますね~
+https://github.com/seattlerb/minitest/blob/7e25922c340fa1cfdbeffaf8f37ec020cf6a4fff/lib/minitest/unit.rb#L1255
+
+puke は location を呼んでるから、~
+https://github.com/seattlerb/minitest/blob/7e25922c340fa1cfdbeffaf8f37ec020cf6a4fff/lib/minitest/unit.rb#L954
+
+この location を書き換えればいいのかと思ったのですが・・・
+
+ LANG:ruby
+ module Minitest
+ class Unit
+ def location e
+ e.backtract.join("\n")
+ end
+ end
+ end
+
+として得られたトレースはこんな感じ。
+
+ LANG:console
+ minitest-4.7.5/lib/minitest/unit.rb:195:in `assert'
+ shoulda-context-1.2.1/lib/shoulda/context/assertions.rb:91:in `safe_assert_block'
+ shoulda-context-1.2.1/lib/shoulda/context/assertions.rb:83:in `assert_rejects'
+ shoulda-context-1.2.1/lib/shoulda/context/context.rb:358:in `block in should_not'
+ shoulda-context-1.2.1/lib/shoulda/context/context.rb:413:in `instance_exec'
+ shoulda-context-1.2.1/lib/shoulda/context/context.rb:413:in `block in create_test_from_should_hash'
+ minitest-4.7.5/lib/minitest/unit.rb:1258:in `run'
+ minitest-4.7.5/lib/minitest/unit.rb:933:in `block in _run_suite'
+ minitest-4.7.5/lib/minitest/unit.rb:926:in `map'
+ minitest-4.7.5/lib/minitest/unit.rb:926:in `_run_suite'
+ minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `block in _run_suites'
+ minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `map'
+ minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites'
+ minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything'
+ minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests'
+ minitest-4.7.5/lib/minitest/unit.rb:1072:in `block in _run'
+ minitest-4.7.5/lib/minitest/unit.rb:1071:in `each'
+ minitest-4.7.5/lib/minitest/unit.rb:1071:in `_run'
+ minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
+ minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun'
+
+バックトレースに TestCase の行が含まれていない???
+
+https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/context.rb#L356
+
+ LANG:ruby
+ def should_not(matcher)
+ name = matcher.description
+ blk = lambda { assert_rejects matcher, subject }
+ self.shoulds << { :name => "not #{name}", :block => blk }
+ end
+
+この lambda で作った blk が shoulds に溜められて、後から順に実行され、そこでエラーが出ると。
+
+これだと例外から行番号を得るのは難しいですね。
+
+むしろ、この :name に行番号を突っ込んじゃえばいいという話かも。
+
+ LANG:ruby
+ module Shoulda
+ module Context
+ class Context # :nodoc:
+
+ def add_caller_line_number(should)
+ caller(2).first =~ /:(\d+):in /
+ should[:name] = should[:name].to_s + " (at line #{$1})"
+ end
+
+ alias :should_without_caller_information :should
+ def should(name_or_matcher, options = {}, &blk)
+ count_before = self.shoulds.count
+ should_without_caller_information(name_or_matcher, options = {}, &blk)
+ if self.shoulds.count > count_before
+ add_caller_line_number(self.shoulds.last)
+ else
+ add_caller_line_number(self.should_eventuallys.last)
+ end
+ end
+
+ alias :should_not_without_caller_information :should_not
+ def should_not(matcher)
+ should_not_without_caller_information(matcher)
+ add_caller_line_number(self.shoulds.last)
+ end
+
+ end
+ end
+ end
+
+これで、
+ LANG:console
+ 3) Failure:
+ AccountControllerTest#test_: with locale ja, without login, GET #home should not
+ set the flash (at line 35).
+ [/var/lib/gems/2.0.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:358]:
+ Did not expect the flash to be set, but was #<ActionDispatch::Flash::FlashHash:0xad041fc ...
+
+などというように、should や should_not に対応する行番号が (at line xxx) の形で付くようになりました。
+
+* 役立ちそうな記事 [#x64e134e]
+
+- http://www.railstips.org/blog/archives/2009/02/21/shoulda-looked-at-it-sooner/
+
* コメント [#yfa709ad]
#article_kcaptcha