简体   繁体   English

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

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

I'm coding a Rails 4 application to learn Rails & testing. 我正在编写一个Rails 4应用程序,以学习Rails和测试。 My program code works as expected, but I can't figure out why I'm getting a no method error when posting to the create method in a Sessions controller test (RSpec v. 3.1.0) Here's the text of the error: 我的程序代码按预期工作,但是我无法弄清楚为什么在Sessions控制器测试(RSpec v.3.1.0)中将其发布到create方法时出现no method错误的原因,这是错误的内容:

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

This is relevant code from my Sessions Controller spec: 这是我的会话控制器规范中的相关代码:

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

This is my Sessions Controller code: 这是我的会话控制器代码:

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

The error says undefined method for nil:NilClass. 该错误表明nil:NilClass的未定义方法。 I'm sure the user is valid. 我确定用户有效。 I can't figure out why posting to the create method is not working in the test scenario. 我不知道为什么在测试场景中无法发布到create方法。 It works as expected in the application. 它可以在应用程序中按预期方式工作。 What am I missing? 我想念什么?

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

The second argument of post is a parameter hash which will be sent to the controller. post的第二个参数是参数哈希,它将被发送到控制器。 You are now passing { email: "testerx@tester-x.net", password: "passwordx" } to post , and obviously there is no session key in the parameter hash. 您现在正在传递{ email: "testerx@tester-x.net", password: "passwordx" }进行post ,显然参数哈希中没有session密钥。 When your controller tries to access paramas[:session][:xxx] , it gets NoMethodError because params[:session] is nil, and nil does not have method [] . 当您的控制器尝试访问paramas[:session][:xxx] ,由于params[:session]为nil且nil没有方法[] ,它会收到NoMethodError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM