class Mongoid::Matchers::HaveSetCallbackMatcher

Create an matcher for set_callback method

Usage:

it { is_expected.to set_callback(:save, :arround) } it { is_expected.to set_callback(:validation, :after) }

Constants

CONTEXTS
OPERATIONS

Public Class Methods

new(*args) click to toggle source
# File lib/matchers/set_callbacks.rb, line 16
def initialize(*args)
  @type = args[0]
  @hook = args[1]
end

Public Instance Methods

description() click to toggle source
# File lib/matchers/set_callbacks.rb, line 32
def description
  "be set_callback(:#{@type}, :#{@hook})"
end
failure_message() click to toggle source
# File lib/matchers/set_callbacks.rb, line 36
def failure_message
  message_error
end
failure_message_when_negated() click to toggle source
# File lib/matchers/set_callbacks.rb, line 40
def failure_message_when_negated
  message_error
end
matches?(klass) click to toggle source
# File lib/matchers/set_callbacks.rb, line 21
def matches?(klass)
  @model = klass

  return false unless \
    context_is_correct? ||
    operation_is_correct? ||
    callback_is_correct?

  testings_presence_callback
end

Private Instance Methods

callback_is_correct?() click to toggle source
# File lib/matchers/set_callbacks.rb, line 54
def callback_is_correct?
  @model.class.respond_to?(:"_#{@type}_callbacks")
end
context_is_correct?() click to toggle source
# File lib/matchers/set_callbacks.rb, line 46
def context_is_correct?
  CONTEXTS.include?(@hook)
end
message_error() click to toggle source
# File lib/matchers/set_callbacks.rb, line 64
      def message_error
        <<-ERROR
  expected #{@model} have method callback :
    set_callback(:#{@type}, :#{@hook}) |document|
      # My callback execution ...
    end
        ERROR
      end
operation_is_correct?() click to toggle source
# File lib/matchers/set_callbacks.rb, line 50
def operation_is_correct?
  OPERATIONS.include?(@type)
end
testings_presence_callback() click to toggle source
# File lib/matchers/set_callbacks.rb, line 58
def testings_presence_callback
  @model.class.send("_#{@type}_callbacks".to_sym).select do |cb|
    cb.filter.eql?(@hook.to_sym)
  end
end