不推荐使用`post_via_redirect`,它将在Rails 5.1中删除

时间:2016-03-23 10:06:49

标签: ruby-on-rails testing rspec-rails railstutorial.org ruby-on-rails-5

我正在浏览Rails教程书,在第8章中,当运行bin / rails测试时,我收到了这条消息:

 DEPRECATION WARNING: `post_via_redirect` is deprecated and will be removed in Rails 5.1. Please use follow_redirect! manually after the request call for the same behavior.

生成此消息的代码位于test/integration/user_signup_test.rb

      test "valid signup information" do
        get signup_path
        assert_difference 'User.count', 1 do
          post_via_redirect users_path, user: { name:  "Example User",
                                        email: "user@example.com",
                                        password:              "password",
                                        password_confirmation: "password" }
        end
        assert_template 'users/show'
      end

我该如何解决?

1 个答案:

答案 0 :(得分:15)

固定代码

test "valid signup information" do
  get signup_path
  assert_difference 'User.count', 1 do
    post users_path, params: { user: { name:  "Example User",
                                        email: "user@example.com",
                                        password:              "password",
                                        password_confirmation: "password" } } 
    follow_redirect!
  end
  assert_template 'users/show'
end

注意:我还按照Rails 5的建议添加了params哈希。