Fetching ‚aktions‘ oder ‚Controller‘ von Helfern spec
-
07-07-2019 - |
Frage
Lassen Sie uns sagen, dass ich den folgenden Code haben in application_helper.rb :
def do_something
if action_name == 'index'
'do'
else
'dont'
end
end
, die etwas tun werden, wenn innerhalb Index Aktion aufgerufen.
F: Wie kann ich schreibe die Helfer-Spezifikation für diesen in application_helper_spec.rb einen Anruf von 'Index' Aktion zu simulieren
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
Lösung
Sie können Stummel aktions Methode zu, was von Ihnen gewünschten Wert:
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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow