ヘルパー仕様から「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