How to check if a module adds a before_filter method if it is enabled by a controller class

I wonder how you would solve this problem?

You want to reorganize the following code

class AController < ActionController::Base
  before_filter :the_method

protected
  def the_method
  end 
end

at

class AController < ActionController::Base
  include TheModule
end

but since you are a BDD enthusiast, you need to write a spec first

describe TheModule do
  context "when included" do
    it "adds #the_method as a before_filter" do

      # insert your code here

    end
  end
end

In other words, the question is: How to write a specification that checks if TheModel # the_method is added as before_filter when it is included in the class (presumably the controller class).

+3
source share
2 answers

I did it like this:

describe TheModule do
  context "when included" do
    it "adds #the_method as a before_filter" do
      instance = Object.new
      instance.class_eval do |klass|
        expects(:before_filter).with(:expensive_method)
        include TheModule
      end
    end
  end
end

Since we do not need to check the behavior before the filter. We can simply check if it is executed correctly.

+1
source

, callback .

@model.methods.include?('before_callback_method_name')

, , .

.

, , , , , , , .

* *

:

@u._validate_callbacks

, , , .

>> u._validate_callbacks.first.class
=> ActiveSupport::Callbacks::Callback
>> u._validate_callbacks.first.kind
=> :before
>> u._validate_callbacks.first.filter
=> :validate_associated_records_for_sites

, .

+1

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


All Articles