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

时间:2016-10-13 18:23:06

标签: ruby-on-rails ruby-on-rails-5

我尝试调整后控制器的集成测试以符合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

1 个答案:

答案 0 :(得分:0)

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

要满足此删除警告,您需要将参数包装在花括号中。这样它被识别为Hash并满足Rails。

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