Pregunta

Digamos que tengo el siguiente código en application_helper.rb :

def do_something
 if action_name == 'index'
   'do'
 else
   'dont'
 end
end

que hará algo si se llama dentro de la acción de índice.

P: ¿Cómo reescribo la especificación de ayuda para esto en application_helper_spec.rb para simular una llamada desde la acción 'index'?

describe 'when called from "index" action' do
  it 'should do' do
    helper.do_something.should == 'do' # will always return 'dont'
  end
end

describe 'when called from "other" action' do
  it 'should do' do
    helper.do_something.should == 'dont'
  end
end
¿Fue útil?

Solución

Puede aplicar el método action_name al valor que desee:

describe 'when called from "index" action' do
  before
    helper.stub!(:action_name).and_return('index')
  end
  it 'should do' do
    helper.do_something.should == 'do'
  end
end

describe 'when called from "other" action' do
  before
    helper.stub!(:action_name).and_return('other')
  end
  it 'should do' do
    helper.do_something.should == 'dont'
  end
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top