![](/img/trans.png)
[英]Rails: How to call ApplicationController method WITH ARGS from a controller's before_action/after_action method INLINE?
[英]Minitest - test ApplicationController before_action in Rails 4
如何在Minitest中測試ApplicationController before_action? 我有一個before_action從url params設置locale但我不知道如何為它設置測試。
您可以通過設置匿名控制器並將規范請求路由到該控制器來實現。
這是一個例子:
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.