統合テストと rak
前回までで、asagao の単体テストと機能テストが全て通りました。
今回は、統合テストを通しましょう。
> rake test:integration
(省略)
1) Failure:
test_linked(InnerLinksTest)
[./test/integration/inner_links_test.rb:22:in `visit'
./test/integration/inner_links_test.rb:28:in `visit'
./test/integration/inner_links_test.rb:25:in `each'
./test/integration/inner_links_test.rb:25:in `visit'
./test/integration/inner_links_test.rb:13:in `test_linked'
/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'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/integration.rb:597:in `run']:
A wrong or broken path </account/show> was found within the page </>.
Expected response to be a <:success>, but was <404>
2 tests, 37 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby -Ilib:test "/usr/lib/ruby/ge...]
(See full trace by running task with --trace)
この連載を追いかけてきた方にとっては簡単ですね。
シングルトン・リソースで説明したように、Rails 2.0 からコントローラ名の規約が少し変わったことが原因です。
エラーメッセージには「トップページに /account/show へのリンクがあるけど、これは間違っているか壊れている」と書いてあります。
ただし、このパスがトップページのどこに現れるのかは分かりません。
どうやって調べるといいでしょうか。
普通は、アプリケーションを起動して、ブラウザでトップページを開いて、ソースコードを調べるのですが、ここでは rak を使ってみましょう。
rak は Ruby で書かれた grep のようなツールです。
rak は gem でインストールします。
> sudo gem install rak
rak はあるディレクトリ以下のファイル群から特定のパターンを含む行をリストアップしてくれます。
account という文字列を含む行が見つかれば、そこが怪しいですね。
> rak account app/views app/views/layouts/application.rhtml 16| <td id="account"> 17| <%= render :partial => 'shared/account' %> app/views/shared/_account.rhtml 2| <% unless params[:controller] == 'account' -%> 4| :controller => '/account', :action => 'show' %> |
ビンゴ!
rak は色付きで分かりやすく結果を出力してくれます。
ただし、Windows で色を表示したい場合は、win32console パッケージを gem でインストールする必要があります。
app/views/shared/_account.rhtml の 2-5 行を次のように修正します。
<% unless params[:controller] == 'accounts' -%>
<%= link_to @current_user.full_name + 'さんのアカウント',
:controller => '/accounts', :action => 'show' %> |
<% end -%>
test:integration タスクを実行すると…
2 tests, 66 assertions, 0 failures, 0 errors
完璧です。
次回からは、asagao を Rails 2.2 的により正しいアプリケーションにするため、ソースコードを見直していくことにしましょう。
(2008/12/22)
記事に関するご質問は、 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)

