ruby-on-rails - ApplicationController Private method's variable rspec testing
问题描述
I have made this code in ApplicationController. This is a method which runs at the start of any method to run. I want to test this method with rspec and want to know that the gender is giving right output or wrong.
this is the route of the code
get ':sex/names/:name_kanji', to: 'names#show'
And this is the application controller:
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
解决方案
I would inspect the instance variables that being set in the before_action. It would look something like this:
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
推荐阅读
- c# - 字节到图像 C# 从 Web 应用程序到 Windows 应用程序
- python - 为什么用python进行多线程mysql查询要花费这么多时间
- java - RabbitMQ 消息过期通知
- mysql - MYSQL - 当 tinyint 更新为 TRUE 时,TRIGGER 从表中插入另一个表值
- php - 为什么当 CURLOPT_SSL_VERIFYHOST = 0 时我的 php curl 不起作用?
- reactjs - react js中的声明式组合与命令式
- jquery - SCRIPT5007: Unable to get value of the property 'keypress': object is null or undefined
- scala - Spark 数据框连接显示意外结果 - 0 行
- summernote - 如何使用summernote在图像上放置文字
- flutter - 在连接到 Internet 时构建 Flutter Web 应用程序会给我一个内部服务器错误。?