Techioz Blog

Rails minitest でセッションをシミュレートするときにルート一致が得られない

概要

No route matches {:action=>"/login", :controller=>"categories", :email=>"[email protected]", :password=>"fardeen"}

これは事件です。

require 'test_helper'

class CategoriesControllerTest < ActionController::TestCase
  setup do
    @category = Category.create(name: "Sports")
    @user=User.create(username: "fardeen", email: "[email protected]",password: "fardeen", admin: true)
    # @controller.session[:user_id] = @user.id
  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(@user)
    get :new
    assert_response :success
  end

  test "should create category" do
     sign_in_as(@user)
    assert_difference('Category.count',1) do
      post :create, category: { name:"Travel" }
    end

ご覧のとおり、私はユーザーを作成しており、サインインしているユーザーのみがカテゴリを編集または作成できるため、この方法でTDDを作成していますが、ルートエラーを通過することはありません。

セッションではなくコントローラーでカテゴリを取得しているのはなぜですか。

私のルートは次のとおりです。

 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
        login GET    /login(.:format)               session#new
              POST   /login(.:format)               session#create
       logout GET    /logout(.:format)              session#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

色々試してみましたが駄目でした。

これは質問された私のカテゴリコントローラーコードです:

class CategoriesController < ApplicationController

  before_action :require_admin, :except=>[:index,:show]
    def new
        @category=Category.new
    end
    
    def create
        @category=Category.create(category_params)
        if @category.save
               flash[:info]="Category has been created successfully!!!"
               redirect_to @category
        else
            render 'new'
        end
    end

    def index
        @categories=Category.paginate(:page => params[:page], :per_page => 1)
    end
    
    def show
        @category=Category.find(params[:id])
    end

    def edit
    end


private

    def category_params
      params.require(:category).permit(:name)
    end
    
   def require_admin
    if !(logged_in? && current_user.admin)
        flash[:alert]="Only Admin can do this!!!"
      redirect_to categories_path
    end
  end
end



解決策

それを試してみてください

get: index, use_route: 'your_route'