Created
October 23, 2015 16:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/controllers/application_controller_spec.rb | |
require 'spec_helper' | |
describe ApplicationController do | |
describe 'class' do | |
it { has_before_filters(:authenticate_user) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/controller_macros.rb | |
module ControllerMacros | |
def has_before_filters *names | |
expect(controller).to have_filters(:before, *names) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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