ruby-on-rails – 从帮助程序规范中获取’action_name’或’cont
发布时间:2020-12-17 03:25:35 所属栏目:百科 来源:网络整理
导读:假设我在application_helper.rb中有以下代码: def do_something if action_name == 'index' 'do' else 'dont' endend 如果在索引操作中调用,它将执行某些操作. 问:如何在application_helper_spec.rb中重写辅助规范以模拟来自’index’动作的调用? describ
假设我在application_helper.rb中有以下代码:
def do_something if action_name == 'index' 'do' else 'dont' end end 如果在索引操作中调用,它将执行某些操作. 问:如何在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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |