Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minitest - test ApplicationController before_action in Rails 4

How can I test ApplicationController before_action in Minitest? I have a before_action that sets locale from url params but I have no idea how to setup the test for it.

like image 983
Leo Net Avatar asked Oct 20 '22 11:10

Leo Net


1 Answers

You can do it by setting up an anonymous controller and routing the spec request to that controller.

Here is an example:

require 'test_helper'

class BaseController < ApplicationController
  def index
    render nothing: true
  end
end

class BaseControllerTest  < ActionDispatch::IntegrationTest
  test 'redirects if user is not logedin' do
    Rails.application.routes.draw do
      get 'base' => 'base#index'
    end

    get '/base'

    assert_equal 302, status
    assert_redirected_to 'http://example.com/login'
    Rails.application.routes_reloader.reload!
  end

  test 'returns success if user is loggedin' do
    Rails.application.routes.draw do
      get 'base' => 'base#index'
    end

    mock_auth!

    get '/base'
    assert_equal 200, status
    Rails.application.routes_reloader.reload!
  end
end
like image 150
golfadas Avatar answered Oct 22 '22 02:10

golfadas