Minitest - 在 Rails 4 中测试 ApplicationController before_action
Minitest - test ApplicationController before_action in Rails 4
如何在 Minitest 中测试 ApplicationController before_action?我有一个 before_action 从 url 参数设置语言环境,但我不知道如何为它设置测试。
您可以通过设置匿名控制器并将规范请求路由到该控制器来实现。
这是一个例子:
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
如何在 Minitest 中测试 ApplicationController before_action?我有一个 before_action 从 url 参数设置语言环境,但我不知道如何为它设置测试。
您可以通过设置匿名控制器并将规范请求路由到该控制器来实现。
这是一个例子:
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