5
如何在Minitest中測試ApplicationController before_action?我有一個before_action設置url params的語言環境,但我不知道如何爲它設置測試。Minitest - 在Rails 4中測試ApplicationController before_action
如何在Minitest中測試ApplicationController before_action?我有一個before_action設置url params的語言環境,但我不知道如何爲它設置測試。Minitest - 在Rails 4中測試ApplicationController before_action
您可以通過設置一個匿名控制器並將spec請求路由到該控制器來完成。
下面是一個例子:
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