도우미 사양에서 'Action_name'또는 'Controller'를 가져 오기
-
07-07-2019 - |
문제
다음 코드가 있다고 가정 해 봅시다. application_helper.rb:
def do_something
if action_name == 'index'
'do'
else
'dont'
end
end
색인 조치 내에서 호출되면 무언가를 할 것입니다.
Q :이를 위해 도우미 사양을 다시 작성하려면 어떻게합니까? application_helper_spec.rb '색인'작업에서 통화를 시뮬레이션하려면?
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