履歴: ソフトウェア/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/26 4:31:48)
@@ -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,365 @@
というのも作っておくと良さそう
+** エラーの生じた 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
+
+このようなメッセージからエラーが生じたテストの場所を特定するのはなかなか手間がかかります。
+
+ごたごたは飛ばして解決策へ飛ぶ場合は [[こちら>#s29326e1]]
+
+*** 試行錯誤 [#c418a6e5]
+
+で、例外のバックトレースをいじって改善できない物か???と考えたわけです。
+
+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 に溜められて、後から順に実行され、そこでエラーが出ると。
+
+これだと例外から行番号を得るのは難しいですね。
+
+*** 解決 [#s29326e1]
+
+むしろ、この :name に行番号を突っ込んじゃえばいいという話かも。
+
+test/shoulda_macros/add_caller_information.rb
+ LANG:ruby
+ #coding: utf-8
+
+ module Shoulda
+ module Context
+ class Context # :nodoc:
+
+ # should ブロックの :name に行番号を書き足す
+ def add_caller_line_number(should)
+ # 現バージョンでは2番目が呼び出し元になるみたい
+ 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) の形で付くようになりました。
+
+caller() は遅いので、called_from を作った、という記事がありました。テストの規模が大きい場合には検討の価値があるかもしれません。(このライブラリ、Ruby 1.8 以外に対応していない?)~
+http://magazine.rubyist.net/?0031-BackTrace#l7 ~
+とはいえ1万個の should を作ってようやく1秒違うかどうかなので、気にするだけ損なのかも。
+
+* assert_XXX [#h14dab47]
+
+|>|>|>|>|~[[mt>https://github.com/seattlerb/minitest/blob/7e25922c340fa1cfdbeffaf8f37ec020cf6a4fff/lib/minitest/unit.rb#L190]] [[AS>https://github.com/rails/rails/blob/4-0-stable/activesupport/lib/active_support/testing/assertions.rb]] [[AS dep>https://github.com/rails/rails/blob/4-1-stable/activesupport/lib/active_support/testing/deprecation.rb]] [[AD>https://github.com/rails/rails/tree/4-0-stable/actionpack/lib/action_dispatch/testing/assertions]] [[AC>https://github.com/rails/rails/blob/4-0-stable/actionpack/lib/action_controller/test_case.rb#L95]] [[sd>https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/assertions.rb]] [[jr>https://github.com/rails/jquery-rails/blob/master/lib/jquery/assert_select.rb]] で実装される assert_xxxx |
+|名前|引数|テスト内容|refuse|実装|
+| assert |test, msg = nil | test | ○ |mt|
+| assert_accepts | matcher, target, options = {} | matcher.matches?(target) | | sd |
+| assert_blank | obj, msg=nil | obj.blank? | | AS |
+| assert_contains | collection, x, extra_msg = "" | x can be regexp | | sd |
+| assert_deprecated | match = nil, &block | | | AS |
+| assert_difference|expression, diff = 1, msg = nil, &block|block 前後で変化||AS|
+| assert_does_not_contain | collection, x, extra_msg = "" | x can be regexp | | sd |
+| assert_dom_equal | expected, actual, message = "" | HTML::Document.new(?).rootを比較 | | AD |
+| assert_dom_not_equal | expected, actual, message = "" | HTML::Document.new(?).rootを比較 | | AD |
+| assert_empty |obj, msg = nil | obj.empty? | ○ |mt|
+| assert_equal |exp, act, msg = nil | exp == act | ○ |mt|
+| assert_generates | expected_path, options, defaults={}, extras = {}, message=nil | | | AD |
+| assert_in_delta |exp, act, delta = 0.001, msg = nil | (act-exp).abs <= delta | ○ |mt|
+| assert_in_epsilon |a, b, epsilon = 0.001, msg = nil | 上の別名 | ○ |mt|
+| assert_includes |collection, obj, msg = nil | collection.include?(obj) | ○ |mt|
+| assert_instance_of |cls, obj, msg = nil | obj.instance_of?(cls) | ○ |mt|
+| assert_kind_of |cls, obj, msg = nil | obj.kind_of?(cls) | ○ |mt|
+| assert_match |matcher, obj, msg = nil | matcher =~ obj | ○ |mt|
+| assert_nil |obj, msg = nil | obj.nil? | ○ |mt|
+| assert_no_difference|expression, msg = nil, &block|block 前後で保存||AS|
+| assert_no_match |matcher, obj, msg = nil | matcher !~ obj | |mt|
+| assert_no_tag | *opts | html_document.find(*opts) | | AD |
+| assert_not |obj, msg = nil|!obj||AS|
+| assert_not_deprecated | &block | | |AS|
+| assert_not_empty |obj, msg = nil | !obj.empty? | |mt|
+| assert_not_equal |exp, act, msg = nil | exp != act | |mt|
+| assert_not_in_delta |exp, act, delta = 0.001, msg = nil | (act-exp).abs > delta | |mt|
+| assert_not_in_epsilon |a, b, epsilon = 0.001, msg = nil | 上の別名 | |mt|
+| assert_not_includes |collection, obj, msg = nil | !collection.include?(obj) | |mt|
+| assert_not_instance_of |cls, obj, msg = nil | !obj.instance_of?(cls) | |mt|
+| assert_not_kind_of |cls, obj, msg = nil | !obj.kind_of?(cls) | |mt|
+| assert_not_nil |obj, msg = nil | !obj.nil? | |mt|
+| assert_not_operator |o1, op, o2 = UNDEFINED, msg = nil | !o1._send_(op, o2) | |mt|
+| assert_not_predicate |o1, op, msg = nil | !o1._send_(op) | |mt|
+| assert_not_respond_to |obj, meth, msg = nil | !obj.respond_to?(meth) | |mt|
+| assert_not_same |exp, act, msg = nil | !exp.equal?(act) | |mt|
+| assert_nothing_raised | | | | |
+| assert_operator |o1, op, o2 = UNDEFINED, msg = nil | o1._send_(op, o2) | ○ |mt|
+| assert_output |stdout = nil, stderr = nil | | |mt|
+| assert_predicate |o1, op, msg = nil | o1._send_(op) | ○ |mt|
+| assert_raise | | | | |
+| assert_raises |*exp | | |mt|
+| assert_recognizes | expected_options, path, extras={}, msg=nil | | | AD |
+| assert_redirected_to | options = {}, message=nil | | | AD |
+| assert_rejects | matcher, target, options = {} | !matcher.matches?(target) | | sd |
+| assert_respond_to |obj, meth, msg = nil | obj.respond_to?(meth) | ○ |mt|
+| assert_response | type, message = nil | | | AD |
+| assert_routing | path, options, defaults={}, extras={}, message=nil | | | AD |
+| assert_same |exp, act, msg = nil | exp.equal?(act) | ○ |mt|
+| assert_same_elements | a1, a2, msg = nil | compare array asif it is set | | sd |
+| assert_select | *args, &block | | | AD |
+| assert_select_email | &block | | | AD |
+| assert_select_encoded | element = nil, &block | | | AD |
+| assert_select_jquery | *args, &block | | | jr |
+| assert_send |[recv, msg, *args], m = nil | recv._send_(msg, *args) | |mt|
+| assert_silent | |assert_output "", "" | |mt|
+| assert_tag | *opts | html_document.find(*opts) | | AD |
+| assert_template | options = {}, message = nil | | | AC |
+| assert_throws |sym, msg = nil | | |mt|
+
+- mt: minitest = https://github.com/seattlerb/minitest/blob/7e25922c340fa1cfdbeffaf8f37ec020cf6a4fff/lib/minitest/unit.rb#L190
+- AS: ActiveSupport = https://github.com/rails/rails/blob/4-0-stable/activesupport/lib/active_support/testing/assertions.rb- AS: ActiveSupport deplication = https://github.com/rails/rails/blob/4-1-stable/activesupport/lib/active_support/testing/deprecation.rb
+- AD: ActionDispatch = https://github.com/rails/rails/tree/4-0-stable/actionpack/lib/action_dispatch/testing/assertions
+- AC: ActionController = https://github.com/rails/rails/blob/4-0-stable/actionpack/lib/action_controller/test_case.rb#L95
+- sd: Shoulda = https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/assertions.rb
+- jr: jquery-rails = https://github.com/rails/jquery-rails/blob/master/lib/jquery/assert_select.rb
+
+AD: with_routing ~
+AD: css_select ~
+
+* shouda の matcher [#gd8d5b05]
+
+#multicolumns
+
+*** [[ActiveModel Matchers>https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers]] [#n7a5004d]
+
+-allow_mass_assignment_of
+-''allow_value''
+-ensure_inclusion_of
+-ensure_exclusion_of
+-ensure_length_of
+-have_secure_password
+-validate_absence_of
+-validate_acceptance_of
+-validate_confirmation_of
+-validate_numericality_of
+-validate_presence_of
+-validate_uniqueness_of
+
+#multicolumns
+
+*** [[ActiveRecord Matchers>https://github.com/thoughtbot/shoulda-matchers#activerecord-matchers]] [#x0e4eaa5]
+
+-accept_nested_attributes_for
+-belong_to
+-have_many
+-have_one
+-have_and_belong_to_many
+-have_db_column
+-have_db_index
+-have_readonly_attribute
+-serialize
+
+#multicolumns
+
+*** [[ActionController Matchers>https://github.com/thoughtbot/shoulda-matchers#actioncontroller-matchers]] [#kdd1429f]
+
+-filter_param
+-permit
+-''redirect_to''
+-''render_template''
+-''render_with_layout''
+-rescue_from
+-''respond_with''
+-route
+-''set_session''
+-''set_the_flash''
+-use_after_filter
+-use_after_action
+-use_around_filter
+-use_around_action
+-use_before_filter
+-use_around_action
+
+#multicolumns(end)
+
+始めからコード側で宣言的に書かれている、例えば has_many なんかを
+わざわざテストしても、ほとんどコピペになるだけで意味は薄いと思います。
+
+なので、上記のように matcher はたくさんある物の、頻繁に使うのは、
+
+ActionModel の
+-allow_value
+-- allow_value('http://foo.com', 'http://bar.com/baz').for(:website_url)
+-- allow_value('2013-01-01').for(:birthday_as_string).on(:create)
+-- allow_value('open', 'closed').for(:state).with_message('State must be open or closed')
+
+ActionController の
+
+-redirect_to
+-- redirect_to :list
+-- redirect_to { posts_path }
+-render_template
+-- render_template 'show'
+-- render_template :show
+-respond_with
+-- respond_with 403
+-- respond_with :success
+-- respond_with 500..600
+-route
+-- route(:get, '/posts').to(controller: 'posts', action: 'index')
+-- route(:get, '/posts/1').to('posts#show', id: 1)
+-set_session
+-- set_session :key
+-- set_session(:key).to("value")
+-- set_session(:key).to{ calc_expectation }
+-set_the_flash
+-- set_the_flash
+-- set_the_flash.to('value')
+-- set_the_flash.to(/regexp/)
+-- set_the_flash[:key]
+-- set_the_flash[:key].to('value')
+-- set_the_flash[:key].to(/regexp/)
+-- set_the_flash[:key].now
+-- set_the_flash[:key].to('value').now
+
+くらいかもしれません。
+
+** matcher の作り方 [#m38d0482]
+
+set_session のコードを見れば一通り分かりそうですね。
+
+https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/action_controller/set_session_matcher.rb
+
+* 役立ちそうな記事 [#x64e134e]
+
+- http://www.railstips.org/blog/archives/2009/02/21/shoulda-looked-at-it-sooner/
+
* コメント [#yfa709ad]
#article_kcaptcha