ApplicationController私有方法的变量rspec测试
ApplicationController Private method's variable rspec testing
我已经在ApplicationController 中编写了这段代码。这是一个在 运行 的任何方法开始时 运行s 的方法。我想用 rspec 测试这个方法,并想知道性别给出的输出是正确的还是错误的。
这是代码的路径
get ':sex/names/:name_kanji', to: 'names#show'
这是应用程序控制器:
class ApplicationController < ActionController::Base
before_action :check_sex
private
def check_sex
@header_color = nil
@header_nav_hash = {'other' => nil, 'boy' => nil, 'girl' => nil}
@page_scoop = params[:sex].presence || 'other'
unless @page_scoop.match(/^(boy|girl|other)$/)
render_404
end
if @page_scoop == "boy" || @page_scoop == "girl"
@gender_base = @page_scoop
end
@header_nav_hash[@page_scoop] = 'is-active'
@header_color = @page_scoop == 'boy' ? 'is-info' : 'is-danger' if @page_scoop != 'other'
end
def render_404
render template: 'errors/error_404' , status: 404 , layout: 'application' , content_type: 'text/html'
end
def render_500
render template: 'errors/error_500' , status: 500 , layout: 'application' , content_type: 'text/html'
end
end
我会检查 before_action 中设置的实例变量。它看起来像这样:
describe 'on GET to :show' do
before do
get :show, params: { <valid params here> }
end
it 'sets @page_scoop' do
expect(assigns(:page_scoop)).to eq 'boy'
end
end
我已经在ApplicationController 中编写了这段代码。这是一个在 运行 的任何方法开始时 运行s 的方法。我想用 rspec 测试这个方法,并想知道性别给出的输出是正确的还是错误的。
这是代码的路径
get ':sex/names/:name_kanji', to: 'names#show'
这是应用程序控制器:
class ApplicationController < ActionController::Base
before_action :check_sex
private
def check_sex
@header_color = nil
@header_nav_hash = {'other' => nil, 'boy' => nil, 'girl' => nil}
@page_scoop = params[:sex].presence || 'other'
unless @page_scoop.match(/^(boy|girl|other)$/)
render_404
end
if @page_scoop == "boy" || @page_scoop == "girl"
@gender_base = @page_scoop
end
@header_nav_hash[@page_scoop] = 'is-active'
@header_color = @page_scoop == 'boy' ? 'is-info' : 'is-danger' if @page_scoop != 'other'
end
def render_404
render template: 'errors/error_404' , status: 404 , layout: 'application' , content_type: 'text/html'
end
def render_500
render template: 'errors/error_500' , status: 500 , layout: 'application' , content_type: 'text/html'
end
end
我会检查 before_action 中设置的实例变量。它看起来像这样:
describe 'on GET to :show' do
before do
get :show, params: { <valid params here> }
end
it 'sets @page_scoop' do
expect(assigns(:page_scoop)).to eq 'boy'
end
end