当我收到堆栈溢出的建议时,我正在集成测试文件中进行功能测试,但是当我创建用户登录测试会话时,我收到以下错误:
Finished tests in 0.273092s, 3.6618 tests/s, 0.0000 assertions/s.
1) Error:
ListCategoriesTest#test_should_create_category:
NoMethodError: undefined method `[]' for nil:NilClass
app/controllers/session_controller.rb:8:in `create'
test/test_helper.rb:16:in `sign_in_as'
test/integration/list_categories_test.rb:20:in `block in <class:ListCategoriesTest>'
ruby
请在集成文件夹下找到我的集成文件(list_categories.rb):
ruby
require 'test_helper'
class ListCategoriesTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
def setup
@category=Category.create(name: "Sports")
# @category2=Category.create(name: "Politics")
@admin_user = User.create(username: "fardeen", email: "[email protected]", password: "fardeen", admin: true)
end
# test "Should show categories listing" do
# get '/categories'
# assert_select "a[href]", category_path(@category) ,{text: @category.name}
# assert_select "a[href]", category_path(@category2),{text: @category2.name}
# end
test "should create category" do
sign_in_as(@admin_user)
assert_difference('Category.count',1) do
post :create, category: { name:"Travel" }
end
assert_redirected_to category_path(Category.last)
end
end
为了登录特定用户,我已将登录编写为测试辅助函数中的代码:
ruby
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
def sign_in_as(user)
post login_path, params:{ session:{"email"=> user.email, "password"=> "fardeen"} }
end
end
这是我的会话控制器(会话不是会话)
ruby
class SessionController < ApplicationController
def new
render 'new'
end
def create
user=User.find_by(email: params[:session][:email].downcase)
if user and user.authenticate(params[:session][:password])
flash[:info]="You have been successfully logged in!!!"
session[:user_id]=user.id
redirect_to user
else
flash[:notice]="Invalid Creds!!!"
render 'new'
end
end
def destroy
session[:user_id]=nil
flash[:info]="You have been logged out!!!"
redirect_to users_path
end
end
我不明白为什么它传递 nil 参数或者为什么会话哈希没有通过 post 请求?
我尝试过各种形式来发送参数,例如:
ruby
post login_path, "session"=>{"email"=> user.email, "password"=> "fardeen"}
或
ruby
post login_path, params:{}, session:{"email"=> user.email, "password"=> "fardeen"}
但他们都不起作用,有些人提出了这样的错误
ruby
1) Error:
ListCategoriesTest#test_should_create_category:
ArgumentError: bad argument (expected URI object or URI string)
test/integration/list_categories_test.rb:22:in `block (2 levels) in <class:ListCategoriesTest>'
test/integration/list_categories_test.rb:21:in `block in <class:ListCategoriesTest>'
ruby
ListCategoriesTest: test_should_create_category
-----------------------------------------------
[1m[36m (1.0ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mCategory Exists (0.0ms)[0m SELECT 1 AS one FROM "categories" WHERE "categories"."name" = 'Sports' LIMIT 1
[1m[36mSQL (28.1ms)[0m [1mINSERT INTO "categories" ("created_at", "name", "updated_at") VALUES (?, ?, ?)[0m [["created_at", Sat, 19 Aug 2023 15:30:44 UTC +00:00], ["name", "Sports"], ["updated_at", Sat, 19 Aug 2023 15:30:44 UTC +00:00]]
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35m (0.0ms)[0m ROLLBACK TO SAVEPOINT active_record_1
Started POST "/login" for 127.0.0.1 at 2023-08-19 21:00:44 +0530
Processing by SessionController#create as HTML
Parameters: {"params"=>{"session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}}
Completed 500 Internal Server Error in 41ms
[1m[36m (3.0ms)[0m [1mrollback transaction[0m
Ruby on Rails 4.0 对
post
方法使用了不同的语法。而不是
def sign_in_as(user)
post login_path, params:{ session:{"email"=> user.email, "password"=> "fardeen"} }
end
使用
def sign_in_as(user)
post login_path, { session: { email: user.email, password: "fardeen" } }
end
顺便说一句:Ruby on Rails 4.0 已经有 9 年多了,并于 6 年前达到了生命周期的终点。我强烈建议您将 Rails 版本更新到当前版本,以避免安全问题,以便能够遵循当前的文档、文章和教程,并能够使用当前的 gem。