gpt4 book ai didi

ruby-on-rails - 如何在测试期间在 Helper 中 stub 函数

转载 作者:行者123 更新时间:2023-12-01 00:38:30 25 4
gpt4 key购买 nike

看看这个辅助函数:

def show_welcome_banner?
(controller_name == 'competition' && action_name == 'index') ||
(controller_name == 'submissions' && action_name == 'show')
end

它期望定义 controller_name 和 action_name 函数。

我试图在我的 RSpec 匹配器中使用它:

describe PageHelper do
it "should know when the welcome banner is to be shown" do
helper.stub!(:controller_name).and_return('index')
show_welcome_banner?.should == false
end
end

但这实际上行不通。

我怎样才能在帮助程序中 stub 函数?也许使用 instance_eval?谢谢!

编辑,尝试使用

controller.stub!(:controller_name).and_return('index')

但是得到了

  1) PageHelper should know when the welcome banner is to be shown
Failure/Error: show_welcome_banner?.should == false
NameError:
undefined local variable or method `controller_name' for #<RSpec::Core::ExampleGroup::Nested_1:0x1059a10b8>
# ./app/helpers/page_helper.rb:16:in `show_welcome_banner?'
# ./spec/helpers/page_helper_spec.rb:7

助手放在spec/helper/page_helper_spec.rb..

最佳答案

你试过吗

stub!(:controller_name).and_return('index')

似乎对我有用:)

关于ruby-on-rails - 如何在测试期间在 Helper 中 stub 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927132/

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