シングルトン・リソース
前回は、blog_entries コントローラの機能テストを通しました。
今日は、account コントローラです。
> ruby -Itest test/functional/account_controller_test.rb
Loaded suite test/functional/account_controller_test
Started
FF....
Finished in 0.262014 seconds.
1) Failure:
test_edit1(AccountControllerTest)
[/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/assertions/selector_assertions.rb:297:in `assert_select'
test/functional/account_controller_test.rb:49:in `test_edit1'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run']:
Expected at least 1 element matching "form[action='/account']", found 0.
<false> is not true.
2) Failure:
test_routing(AccountControllerTest)
[test/functional/account_controller_test.rb:18:in `test_routing'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run']:
The generated path <"/account/show"> did not match <"/account">
6 tests, 17 assertions, 2 failures, 0 errors
今回は、ページネーションには関係ないようですね。
テストメソッド test_routing から見ていきましょう。失敗は次の箇所で起きています。
assert_generates '/account',
{ :controller => 'account', :action => 'show' }
これは、Rails 2.0 からシングルトン・リソースのコントローラの名前が単数形から複数形に変わったことによるものです。
account リソースに対応するコントローラは account_controller.rb ではなく、accounts_controller.rb になりました。そのため、ルーティングが混乱してしまったのです。
シングルトン・リソースとは
ある文脈において、Web アプリケーションにひとつしかないリソースのこと。
例えば、「アカウント」というリソースは複数存在しますが、あるユーザーがログインした状態において、「私のアカウント」というリソースはひとつしか存在しません。
Rails では、/accounts という URL パスで「アカウントのリスト」を表し、/accounts/99 で「id が 99 のアカウント」を表し、/account で「私のアカウント」を表します。
Rails 1.2 時代には、普通のリソースとシングルトン・リソースとでコントローラを別々に用意する必要がありましたが、Rails 2.0 以降は同じコントローラで処理できるようになりました。
以下の手順でテストが通ります。
/app/controllers/account_controller.rbのファイル名をaccounts_controller.rbに変更します。accounts_controller.rbを開いて、クラス名をAccountControllerからAccountsControllerに変更します。/app/views/accountディレクトリの名前をaccountsに変更します。/test/functional/account_controller_test.rbのファイル名をaccounts_controller_test.rbに変更します。accounts_controller_test.rbを開いて、次のように修正します。- 2-5行目を削除します。
- クラス名を
AccountControllerTestからAccountsControllerTestに変更します。 setupメソッドの中で使われている、AccountControllerをAccountsControllerに変更します。test_routingメソッドを次のように修正します。
def test_routing
assert_generates '/account',
{ :controller => 'accounts', :action => 'show' }
assert_generates '/account/edit',
{ :controller => 'accounts', :action => 'edit' }
assert_generates '/account',
{ :controller => 'accounts', :action => 'update' }
end
test:functionals タスクを実行すると…
92 tests, 315 assertions, 0 failures, 4 errors
失敗の数が 2 から 0 に減りました。残りは、エラー 4 個です。本日はここまでとしましょう。
(2008/12/21)
記事に関するご質問は、 hermes@oiax.jp までメールでお送りください。
ウェブサイト構築の発注先を検討されているお客様は、ご相談フォームをご利用ください。
- はじめに
- rake rails:update (2008/12/15)
- クッキーストア (2008/12/16)
- ページネーション (2008/12/17)
- 機能テスト冒頭部分の修正、等 (2008/12/19)
- blog_entries コントローラの修正 (2008/12/20)
- シングルトン・リソース (2008/12/21)
- 一気に機能テストを全部通す (2008/12/21)
- 統合テストと rak (2008/12/22)
- 国際化(i18n)の第一歩 (2008/12/26)
- Cookie を使ったロケールの切り替え (2009/01/04)
- ActiveRecord モデルのフィールド名の国際化 (2009/01/08)
- エラーメッセージの国際化(1) (2009/01/09)
- エラーメッセージの国際化(2) (2009/01/10)
- エラーメッセージの国際化(3) (2009/01/17)
- エラーメッセージの国際化(4) (2009/01/24)

