I am using a sweeper to clear a fragment cache and everything is working fine in development, but I am receiving an error in our specs

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)>'

this is the sweeper

class NavigationSweeper < ActionController::Caching::Sweeper
  observe Category, Product, Series

  def after_save(record)
    expire_fragment 'navigation'
  end
end

and this is where I am using it in the controller

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

this is where it fails in the spec (multiple instances)

pickers = FactoryGirl.create(:category, :name => "Pickers")

anyone have any idea why it might not be finding that method?

有帮助吗?

解决方案

Okay, seems the best strategy here was to stub the method like so in the spec_helper.rb

RSpec.configure do |config|
  config.before(:each) do
    NavigationSweeper.any_instance.stub(:expire_fragment)
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top