发布到 create 方法会导致 Sessions Controller 测试出错 (RSpec)

Posting to the create method causes error in Sessions Controller test (RSpec)

我正在编写一个 Rails 4 应用程序来学习 Rails 和测试。我的程序代码按预期工作,但我无法弄清楚为什么在会话控制器测试 (RSpec v. 3.1.0) 中发布到创建方法时出现无方法错误这是文本错误:

Failure/Error: post :create, email: "testerx@tester-x.net", password: "passwordx"
 NoMethodError:
   undefined method `[]' for nil:NilClass

这是我的会话控制器规范中的相关代码:

describe "POST create" do
context "with correct credentials" do

let!(:user) { User.create(user_name: "Testerx", email: "testerx@tester-x.net", password: "passwordx", password_confirmation: "passwordx", workout_enthusiast: "true" ) }

it "redirects to user show page" do         
    post :create, email: "testerx@tester-x.net", password: "passwordx"
    expect(response).to be_redirect
    expect(response).to redirect_to(users_show_path)
end

这是我的会话控制器代码:

class SessionsController < ApplicationController
def new
end

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
  # Logs the user in and redirects to the user's show page.
  log_in user
  params[:session][:remember_me] == '1' ? remember(user) : forget(user)      
  redirect_to user
else      
  flash.now[:danger] = 'Invalid email/password combination' 
  render 'new'
end
end

def destroy
log_out if logged_in?    
redirect_to root_url
end
end

错误提示 nil:NilClass 未定义方法。我确定用户是有效的。我不明白为什么发布到创建方法在测试场景中不起作用。它在应用程序中按预期工作。我错过了什么?

post :create, email: "testerx@tester-x.net", password: "passwordx" 更改为 post :create, session: { email: "testerx@tester-x.net", password: "passwordx" }

post的第二个参数是一个参数散列,它将被发送到控制器。您现在将 { email: "testerx@tester-x.net", password: "passwordx" } 传递给 post,显然参数哈希中没有 session 键。当您的控制器尝试访问 paramas[:session][:xxx] 时,它会收到 NoMethodError,因为 params[:session] 为 nil,而 nil 没有方法 [].