How to register rail controller filters during rspec controller tests

Say I have a controller with many filters. In the controller test, the code never gets into the #create method, although in the specification I do post: create. In my case, it was that

before_filter: user_signed_in

was not checked, but the test log did not indicate this, so I had to figure it out by turning on / off all filters in turn. Is it possible to get Rails to do some detailed logging when it processes each filter in turn and outputs it to the test log?

+3
source share
2 answers

. , , , . .

ActionController, ApplicationController , :

class ApplicationController < ActionController::Base

  def self.before_filter_with_logging(*arguments, &block)
    filters, conditions =
      ActionController::Filters::FilterChain.extract_options(arguments, &block)
    filter_method_names = filters.map { |fm| fm.to_s }.join(', ')
    logger.info "Setting up before_filter to #{filter_method_names}"

    filter_methods_with_logging =
      filters.map { |fm| (fm.to_s + "_with_logging").to_sym }

    filters.each { |fm|
      define_method( (fm.to_s + "_with_logging").to_sym ) { |*args|
        logger.info "Calling before_filter #{fm}"

        if args.empty?
          send fm
        else
          send fm, args
        end
      }
    }
    before_filter_without_logging(filter_methods_with_logging, &block)
  end

  class << self
    alias_method_chain :before_filter, :logging
  end

  before_filter :your_first_before_filter

Tomáš Pospíšek

+5

, logger.info ":user_signed_in called" . puts ":user_signed_in called", rspec. , , test.log.

0

Source: https://habr.com/ru/post/1791094/


All Articles