Skip to content

Instantly share code, notes, and snippets.

@Bartuz
Created October 23, 2015 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bartuz/cbe41331a6239c19a93d to your computer and use it in GitHub Desktop.
Save Bartuz/cbe41331a6239c19a93d to your computer and use it in GitHub Desktop.
before/after filters/actions rspec testing. Credits to: http://stackoverflow.com/a/20776916/2047418
# spec/controllers/application_controller_spec.rb
require 'spec_helper'
describe ApplicationController do
describe 'class' do
it { has_before_filters(:authenticate_user) }
end
end
# spec/support/controller_macros.rb
module ControllerMacros
def has_before_filters *names
expect(controller).to have_filters(:before, *names)
end
end
# spec/support/matchers/have_filters.rb
RSpec::Matchers.define :have_filters do |kind, *names|
match do |controller|
filters = controller._process_action_callbacks.select{ |f| f.kind == kind }.map(&:filter)
names.all?{ |name| filters.include?(name) }
end
end
# spec/spec_helper.rb
RSpec.configure do |config|
config.include ControllerMacros, type: :controller
end
and than you can just use it in controller specs like this:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment