RSpec: How to test a helper method that calls a private helper method from the controller?
Here's what I have:
An application helper method that calls a controller helper method (private) from it.
Code:
ApplicationHelper:
def ordenar(coluna, titulo = nil)
titulo ||= coluna.titleize
css_class = (coluna == **coluna_ordenacao**) ? "#{**direcao_ordenacao**}" : "ordenavel"
direcao = (coluna == **coluna_ordenacao** and **direcao_ordenacao** == "asc") ? :desc : :asc
link_to titulo, {:sort => coluna, :direction => direcao}, {:class => css_class}
end
ApplicationController:
helper_method :coluna_ordenacao, :direcao_ordenacao
private
def coluna_ordenacao
return params[:sort] if params[:sort] and params[:sort].split(' ').size == 1
return :created_at
end
def direcao_ordenacao
return %w[asc desc].include?(params[:direction]) ? params[:direction] : :desc
end
And here is the problem: 开发者_如何学JAVAThe coluna_ordenacao
and direcao_ordenacao
methods can't be called from RSpec, it gives me the following error:
undefined local variable or method `coluna_ordenacao' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x7fbf0a9fe3d8>
Is there any way for that to work? Btw I'm testing the helper, not the controller
You can access private methods in tests using .send(:private_methods_name)
see documentation
精彩评论