Извлечение 'action_name' или 'controller' из спецификации помощника
-
07-07-2019 - |
Вопрос
Допустим, у меня есть следующий код в application_helper.rb :
def do_something
if action_name == 'index'
'do'
else
'dont'
end
end
который будет делать что-то, если вызывается в рамках действия index. Р>
В: Как мне переписать вспомогательную спецификацию для этого в application_helper_spec.rb , чтобы имитировать вызов из действия '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
Решение
Вы можете присвоить методу action_name любое значение, которое хотите:
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
Не связан с StackOverflow