(Rails 5) 为什么此集成测试关键字参数参数会导致弃用警告?

(Rails 5) Why is this integration test keyword params argument causing a deprecation warning?

我已经尝试调整我的 post 控制器的集成测试以符合 Rails 5 关键字参数结构,但我仍然收到弃用警告。我已经观看了几个视频并通读了文档,但我仍然无法发现它。知道我的语法哪里错了吗?

作为一个还在摸索中的程序员,在此先感谢您的帮助!如果您希望我分享任何其他代码片段,请告诉我!

编辑:添加弃用警告

弃用警告

DEPRECATION WARNING: ActionDispatch::IntegrationTest HTTP request methods will accept only
the following keyword arguments in future Rails versions:
params, headers, env, xhr, as

Examples:

get '/profile',
  params: { id: 1 },
  headers: { 'X-Extra-Header' => '123' },
  env: { 'action_dispatch.custom' => 'custom' },
  xhr: true,
  as: :json
 (called from non_kwarg_request_warning at /home/linux/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0.1/lib/action_dispatch/testing/integration.rb:309)

posts_controller_test.rb

require 'test_helper'

class PostsControllerTest < ActionDispatch::IntegrationTest

  def setup
    @user1 = users(:user1)
  end

  ....

  test "get posts index for authenticated users" do
    get login_path
    post sessions_path,
      params: { email: "user1@example.com",
                password: "password" }  

      # For some reason, use of the @user1 values does not log in the user...
      # params: { email: @user1.email,
      #           password: @user1.password_digest }

    get posts_path
    assert_response :success # Unauth user would redirect to login page
  end

end

users.yml

user1:
  name: user1
  email: user1@example.com
  password_digest: <%= BCrypt::Password.create "password" %>

user2:
  name: user2
  email: user2@example.com
  password_digest: <%= BCrypt::Password.create "password" %>

sessions_controller.rb

class SessionsController < ApplicationController

....

  def create
    user = User.find_by(email: params[:email])
    if user && user.authenticate(params[:password])
      session[:user_id] = user.id
      redirect_to root_url, notice: "Log in successful!"
    else
      flash.now.alert = "Invalid email or password."
      render 'new'
    end
  end

....

end

posts_controller.rb

class PostsController < ApplicationController

  before_action :authenticate_user!

  def index
    user_ids = current_user.timeline_user_ids
    @posts = Post.where(user_id: user_ids)
              .order("created_at DESC")
  end

  def show
    @post = Post.find(params[:id])
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  include SessionsHelper
  protect_from_forgery with: :exception

  private

    def current_user
      if session[:user_id]
        @current_user ||= User.find(session[:user_id])
      end
    end
    helper_method :current_user

    def authenticate_user!
      redirect_to login_path unless current_user
    end
end

routes.rb

Rails.application.routes.draw do
  resources :comments
  resources :post_images
  resources :post_texts
  resources :posts
  resources :users
  resources :sessions

  get 'signup', to: 'users#new', as: 'signup'

  get 'login', to: 'sessions#new', as: 'login'
  get 'logout', to: 'sessions#destroy', as: 'logout'

  root 'posts#index'
end

您的弃用警告似乎必须执行 Rails 5.0.0.1 检测关键字参数的方式。为什么会这样,我也不知道。

为了满足这个 depraction 警告,您需要将参数括在花括号中。这样就被识别为一个Hash,满足Rails。

 post sessions_path, { params: { email: "user1@example.com", password: "password" } }