我正在使用清理器清理碎片缓存,开发过程中一切正常,但我在我们的规范中收到了一个错误
2) Admin - Categories #index displays all categories
Failure/Error: create_basic_category_set
NoMethodError:
undefined method `expire_fragment' for #<NavigationSweeper:0x007fdc01a10970 @controller=nil>
# ./app/sweepers/navigation_sweeper.rb:5:in `after_save'
# ./spec/support/utilities.rb:21:in `create_basic_category_set'
# ./spec/features/admin/categories_spec.rb:5:in `block (2 levels) in <top (required)>'
这是清扫车
class NavigationSweeper < ActionController::Caching::Sweeper
observe Category, Product, Series
def after_save(record)
expire_fragment 'navigation'
end
end
这就是我在控制器中使用它的地方
class Admin::CategoriesController < Admin::BaseController
before_filter :set_up_nav_array
cache_sweeper :navigation_sweeper, only: [ :destroy, :update, :create, :update_positions ]
def index
@roots = Category.roots
end
这就是它在规范(多个实例)中失败的地方
pickers = FactoryGirl.create(:category, :name => "Pickers")
有人知道为什么它可能找不到这种方法吗?
好吧,这里最好的策略似乎是在spec_helper.rb 中这样存根方法
RSpec.configure do |config|
config.before(:each) do
NavigationSweeper.any_instance.stub(:expire_fragment)
end
end