【129日目】【1日20分のRailsチュートリアル】【第10章】アカウント有効化の統合テストを追加する
今日は「10.1.4 有効化のテストとリファクタリング」から。
10.1.4 有効化のテストとリファクタリング
この節では、アカウント有効化の統合テストを追加します。
正しい情報でユーザー登録を行った場合のテスト (リスト7.26) は既にあるので、7.4.4で開発したテストに若干手を加えることにします。
setupメソッドを追加して、
“invalid signup information” はそのまま?(でも警告表示のテストが抜けてたので追加した)
“valid signup information with account activation"は後半新規追加。
test/integration/users_signup_test.rb
class UsersSignupTest < ActionDispatch::IntegrationTest def setup ActionMailer::Base.deliveries.clear end : test "valid signup information with account activation" do : post users_path, user: { name: "Example User", : assert_equal 1, ActionMailer::Base.deliveries.size user = assigns(:user) assert_not user.activated? # 有効化していない状態でログインしてみる log_in_as(user) assert_not is_logged_in? # 有効化トークンが不正な場合 get edit_account_activation_path("invalid token") assert_not is_logged_in? # トークンは正しいがメールアドレスが無効な場合 get edit_account_activation_path(user.activation_token, email: 'wrong') assert_not is_logged_in? # 有効化トークンが正しい場合 get edit_account_activation_path(user.activation_token, email: user.email) assert user.reload.activated? follow_redirect! assert_template 'users/show' assert is_logged_in? end
有効化トークンに関するテスト、この順番が重要なんだろうな。正しい場合からテストすると期待してる動作にはならないもんね。たぶん
リスト10.31のassignsメソッドは本チュートリアル初登場です。
第8章の演習 (8.6) で説明したように、assignsメソッドを使用すると、対応するアクション内にあるインスタンス変数にアクセスできるようになります。
Usersコントローラのcreateアクションでは@userというインスタンス変数が定義されていて、そのインスタンス変数にアクセスできる、ってことらしい。
うーん、難しいな…。
テストは通ることを確認。
$ bundle exec rake test 43 tests, 185 assertions, 0 failures, 0 errors, 0 skips
今日の学習時間は【23分】。