gpt4 book ai didi

ruby-on-rails - 从助手规范中获取 'action_name' 或 'controller'

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:57 26 4
gpt4 key购买 nike

假设我在 application_helper.rb 中有以下代码:

def do_something
if action_name == 'index'
'do'
else
'dont'
end
end

如果在 index 操作中调用,它将执行某些操作。

问:如何在 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 方法 stub 为您想要的任何值:

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

关于ruby-on-rails - 从助手规范中获取 'action_name' 或 'controller',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com