我已经在ApplicationController中编写了这段代码。这是在任何要运行的方法开始时运行的方法。我想用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
答案 0 :(得分:0)
我将检查在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