문제

다음 코드가 있다고 가정 해 봅시다. 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top