Récupération de 'nom_action' ou de 'contrôleur' ??depuis la spécification d'assistance

StackOverflow https://stackoverflow.com/questions/280548

  •  07-07-2019
  •  | 
  •  

Question

Supposons que j'ai le code suivant dans application_helper.rb :

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

qui fera quelque chose s'il est appelé dans l'action d'index.

Q: Comment puis-je réécrire la spécification d'assistance pour cela dans application_helper_spec.rb afin de simuler un appel à partir de l'action '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
Était-ce utile?

La solution

Vous pouvez remplacer la méthode action_name par la valeur de votre choix:

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top