在這個輔助函數請看:如何存根在輔助功能測試
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
助手放在規格/幫手/ page_helper_spec.rb ..
是的,那有效!謝謝! – 2011-06-20 07:16:53