Getting routes error in test helper for simulation a session

33 Views Asked by At

I have been trying to enforce permissions that admin should only create articles. I am writing the tests on minitest. The thing is that I have created the user in categories controller test file and have created the sign in as function to make a new test user session but I am getting this error:

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'

Please find my categories controller test:

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

And the sign_in_as helper method:

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

Please find my routes as well:

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

I dont understand the login_path refers to as login , why it is showing that routing error?

I have really tried a lot of things.

0

There are 0 best solutions below