私の中でapplication_controller.rb
:
helper_method :current_brand
def current_brand
@brand ||= Brand.find_by_organization_id(current_user.organization_id)
end
私のヘルパーでsomething_helper.rb
def brands
return [] unless can? :read, Brand
# current_brand is called
end
の仕様を書いていて、something_helper
スタブしたいcurrent_brand
describe SomethingHelper do
before :each do
helper.stub!(:can?).and_return(true) # This stub works
end
it "does the extraordinary" do
brand = Factory.create(:brand)
helper.stub!(:current_brand).and_return(brand) # This stub doesnt work
helper.brands.should_not be_empty
end
end
結果はNameError:
undefined local variable or method 'current_brand' for #<#<Class:0x000001068fd188>:0x0000010316f6f8>
stub!
私はオンself
とcontroller
同様にやってみました。奇妙なことに、 にスタブするself
と、helper.stub!(:can?).and_return(true)
が登録解除されます。