ruby-on-rails - `post_via_redirect` 已弃用,将在 Rails 5.1 中删除

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

我正在阅读 Rails 教程书和第 8 章,当运行 bin/rails test 时,我收到了这条消息:

 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

我该如何解决?

最佳答案

固定代码

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 散列。

关于ruby-on-rails - `post_via_redirect` 已弃用,将在 Rails 5.1 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36175098/

相关文章:

php - Ruby on Rails 或 PHP... 不确定

mysql - 有没有办法从 Ruby-on-Rails 模型中获取数据库中的数据

ruby-on-rails - 在haml部分模板处理上缺少keyword_end

java - 模拟具有通用(?扩展集合)返回类型的方法时遇到问题

testing - 如何期望 url 在 TestCafe 中重定向?

ruby-on-rails-3 - 我如何使用 rspec 来测试正在救援和重定向的路由?

ruby-on-rails-3 - 期望 Rspec 在 Controller 中引发异常

ruby-on-rails - ActiveStorage::IntegrityError Rspec 附加文件

ruby-on-rails - Capistrano 3,使用上传!在 lib/capistrano/tasks 的任务中

git - 对于 Codeception 测试,git 中应该提交或忽略什么?