Rodrigo Castro
Rodrigo Castro

Reputation: 91

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: The 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

Upvotes: 9

Views: 3128

Answers (1)

fuzzyalej
fuzzyalej

Reputation: 5973

You can access private methods in tests using .send(:private_methods_name)

see documentation

Upvotes: 2

Related Questions