提问人:Fardeen 提问时间:8/18/2023 最后编辑:Fardeen 更新时间:8/18/2023 访问量:25
在模拟会话的测试帮助程序中获取路由错误
Getting routes error in test helper for simulation a session
问:
我一直在尝试强制执行管理员只能创建文章的权限。我正在minitest上编写测试。问题是我已经在类别控制器测试文件中创建了用户,并创建了登录身份函数来制作新的测试用户会话,但我收到此错误:
ruby
ActionController::UrlGenerationError: No route matches {:action=>"/login_categories", :controller=>"categories", :params=>{:email=>"[email protected]", :password=>"fardeen"}}
test/test_helper.rb:16:in `sign_in_as'
请找到我的类别控制器测试:
ruby
require 'test_helper'
class CategoriesControllerTest < ActionController::TestCase
setup do
@category = Category.create(name: "Sports")
** @admin_user = User.create(username: "fardeen", email: "[email protected]",password: "fardeen", admin: true)**
end
test "should get index" do
get :index
assert_response :success
# assert_not_nil assigns(:categories)
end
test "should get new" do
** sign_in_as(@admin_user)**
get :new
assert_response :success
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
test "should not create category if not admin" do
assert_no_difference('Category.count') do
post :create, category: { name:"Travel" }
end
assert_redirected_to categories_path
end
test "should show category" do
get :show, id: @category
assert_response :success
end
test "should get edit" do
sign_in_as(@admin_user)
get :edit, id: @category
assert_response :success
end
end
以及 sign_in_as helper 方法:
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: {email: user.email, password: "fardeen"}
end
end
也请找到我的路线:
ruby
Prefix Verb URI Pattern Controller#Action
root GET / pages#Welcome
about GET /about(.:format) pages#about
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
login GET /login(.:format) session#new
POST /login(.:format) session#create
logout GET /logout(.:format) session#destroy
controller_login POST /controller/login(.:format) session#create
我不明白login_path指的是登录,为什么它显示路由错误?
我真的尝试了很多东西。
答: 暂无答案
评论