Domanda

Diciamo che ho il seguente codice in application_helper.rb :

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

che farà qualcosa se chiamato all'interno dell'azione indice.

Q: Come posso riscrivere le specifiche di supporto per questo in application_helper_spec.rb per simulare una chiamata dall'azione 'indice'?

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
È stato utile?

Soluzione

Puoi stub il metodo action_name su qualunque valore tu voglia:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top