Functional Test でリクエスト発行時(GET/POST/PUT/DELETE)に before_filter/after_filter をかける方法
Functional Test ですべての @response.body と @response.headers の内容をチェックしたくて、リクエスト発行時に after_filter 的なことをしたので、方法をメモ。
以下の記事が非常に参考になりました。
functional test をめぐる冒険 - Rails で行こう!
やってることは、単純に get とか post とかの内部で呼ばれてる process というメソッドを、RAILS_ROOT/test/test_helper 内で書き換えているだけ。
LANG : RUBY
-
def process_with_assert_no_wapi(action, parameters = nil, session = nil, flash = nil)
-
process_without_assert_no_wapi(action, parameters, session, flash)
-
assert_no_wapi
-
end
-
alias_method :process_without_assert_no_wapi, :process
-
alias_method :process, :process_with_assert_no_wapi
以下のような functional test があった場合、get(:index) と get(:show, ...) の前後で自動的にフィルタをかけています。
LANG : RUBY
-
require File.dirname(__FILE__) + '/../test_helper'
-
-
class UsersControllerTest <ActionController::TestCase
-
fixtures :users
-
-
def test_index
-
get(:index)
-
end
-
-
def test_show
-
get(:index, :user_id => 'nov')
-
end
-
end
でも本当は、こんなテスト書かなくていいような設計が一番いいと思うんだけどねぇ。。。












この記事がお役に立ちましたら、一言コメントもらえると嬉しいですm_ _m