RSpec.configure do |config| config.around(:each, mock_auth: lambda { |v| !!v }) do |example| options = example.metadata[:mock_auth] klass = case options when TrueClass User when Hash options[:class] || User else options end underscored_class_name = klass. name[/.*::(\w+)\z/, 1]. gsub(/([a-z])([A-Z])/, '\1_\2'). downcase current_class_method = if options.is_a?(Hash) && options[:method] options[:method] else :"current_#{underscored_class_name}" end instance = if options.is_a?(Hash) && options[:strategy] == :instance klass.new else FactoryGirl.create(underscored_class_name.to_sym) end authentication_method = if options.is_a?(Hash) && options[:authentication_method] options[:authentication_method] else :"authenticate_#{underscored_class_name}!" end described_class_instance = described_class.new if described_class_instance.respond_to?(authentication_method, true) described_class.skip_before_action authentication_method elsif described_class_instance.respond_to?(:authenticate, true) described_class.skip_before_action :authenticate end example.example_group_instance.class.let(current_class_method) { instance } described_class.send(:define_method, current_class_method) do instance end example.run described_class.send(:remove_method, current_class_method) end end