简体   繁体   English

Minitest - 在Rails 4中测试ApplicationController before_action

[英]Minitest - test ApplicationController before_action in Rails 4

How can I test ApplicationController before_action in Minitest? 如何在Minitest中测试ApplicationController before_action? I have a before_action that sets locale from url params but I have no idea how to setup the test for it. 我有一个before_action从url params设置locale但我不知道如何为它设置测试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM