Pergunta

Vamos dizer que eu tenho o seguinte código no application_helper.rb :

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

que vai fazer alguma coisa, se chamado dentro action index.

Q: Como faço para reescrever a especificação ajudante para isso em application_helper_spec.rb para simular uma chamada de ação '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
Foi útil?

Solução

Você pode stub ação de método para o valor que você quer:

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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top