Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

601
Visualizações
Ruby on Rails Testing With Devise Authorization Helpers, Wrong Number of Arguments in Test Helper

I have a basic Rails Scaffold, all of the controller actions are protected by a filter which determines if the current user is an admin. Rather than signing in admin & non-admin users during each controller test I decided to create a helper which logs in a users and asserts a response. All of the controller test are working properly but I'm getting a funky wrong number of arguments error in my helper functions( shown below ).

Controller snippet:

class QuotesController < ApplicationController
  before_action :set_quote, only: [:show, :edit, :update, :destroy]
  before_action :admin_filter

  # GET /quotes
  def index
    @quotes = Quote.all
  end
#... rest of actions...
  private
   # Returns true if user is admin
  def is_admin?
    current_user.boss if user_signed_in?
  end

  # Redirect If not Admin
  def admin_filter
    redirect_to root_path unless is_admin?
  end
end

Test Helpers (reduce redundancy of signing users in and out):

# test_helper.rb
class ActiveSupport::TestCase
  include Devise::Test::IntegrationHelpers

  # Add more helper methods to be used by all tests here...
  module AuthTesting

    # compacts admin_access and unsigned_no_admin to one test
    def test_authorization(path, admin, non_admin) # Error @ this line, stacktrace ends 
      admin_access(path, admin)
      unsigned_no_admin_no_access(path, non_admin)
    end

    # asserts redirect for not signed in/ non admin users
    def unsigned_no_admin_no_access(path, non_admin)
      get path
      assert_response :redirect
      sign_in non_admin
      get path
      assert_response :redirect
      sign_out non_admin
    end

    # Asserts that admin has access 
    def boss_access(path, admin)
      sign_in admin
      get path
      assert_response :success
      sign_out admin # prevents test_access from having leftover signed in admin
    end

  end
end

Test Snippet (all controller actions working properly & test passing)

class QuotesControllerTest < ActionDispatch::IntegrationTest
  include AuthTesting

  setup do
    @quote = quotes(:one)
    @admin = users :jack
    @non_admin = users :phil
  end

  test 'should get index only if admin' do
    test_authorization(quotes_path, @admin, @non_admin) # green
  end
#... rest of test ...
end

The error when running my test:

ERROR["test_authorization", #<Minitest::Reporters::Suite:0x00007f8f009fa7e0 @name="QuotesControllerTest">, 0.3059480000000008]
 test_authorization#QuotesControllerTest (0.31s)
Minitest::UnexpectedError:         ArgumentError: wrong number of arguments (given 0, expected 3)
            test/test_helper.rb:20:in `test_authorization'

Why am I getting this error but still having all my test work properly? Did I not create test helpers properly?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Don't start names of your helper methods with test_.

This is a convention in Minitest: everything that starts with test_ is a test. And so it will be invoked as such, without arguments.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda