请看一下这个助手函数:
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但这不管用。
我如何在助手内部设置函数的存根?也许使用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..中
发布于 2011-06-19 08:35:04
你试过吗
stub!(:controller_name).and_return('index')似乎为我工作过:)
发布于 2011-05-08 17:45:28
你在你的Rspec里试过这样的东西吗?
controller.stub!(:controller_name).and_return('index')帮助程序只是ApplicationController中包含的模块。我相信这种方法来自于ActionController::Base。
发布于 2017-03-29 20:20:12
rspec-rails 3.5
allow(helper).to receive(:controller_name) { 'submissions' }https://stackoverflow.com/questions/5927132
复制相似问题