class Mongoid::Matchers::HaveCallbackMatcher

Constants

KINDS

Ensure that the given model has a callback defined for the given method/s callback(:method1, :method2).after(:validation).on(:create)

@methods       @kind  @operation     @context

Public Class Methods

new( *args ) click to toggle source

Set methods to look for

# File lib/matchers/callbacks.rb, line 11
def initialize( *args )
  @methods = args || []
end

Public Instance Methods

description() click to toggle source
# File lib/matchers/callbacks.rb, line 55
def description
  msg = "call #{@methods.join(", ")}"
  msg << " #{@kind} #{@operation}" if @operation
  msg << " on #{@context}" if @context
  msg
end
failure_message() click to toggle source
# File lib/matchers/callbacks.rb, line 47
def failure_message
  message( true )
end
failure_message_when_negated() click to toggle source
# File lib/matchers/callbacks.rb, line 51
def failure_message_when_negated
  message( false )
end
matches?( klass ) click to toggle source
# File lib/matchers/callbacks.rb, line 30
def matches?( klass )
  return false unless @kind
  return false if @no_op = !klass.class.respond_to?( :"_#{@operation}_callbacks" )

  @guess = nil
  @methods.each do |method|
    filter = klass.class.send( :"_#{@operation}_callbacks" ).detect do |callback|
      # Save callback instance in order to print information
      # about it in case of failure
      @guess = callback if callback.filter == method
      check_filter?(callback, method) and check_kind?(callback, @kind) and check_context?(callback, @context)
    end

    return false unless filter
  end
end
on( action ) click to toggle source

Set on condition

# File lib/matchers/callbacks.rb, line 25
def on( action )
  @context = action
  self
end

Protected Instance Methods

message( should ) click to toggle source
# File lib/matchers/callbacks.rb, line 63
def message( should )
  return "Invalid operation. Use :initialize, :build, :validation,"\
         ":create, :find, :update, :upsert, :save or :destroy" if @no_op

  if @kind
    msg =  "Expected method#{@methods.size > 1 ? 's' : ''} #{@methods.join(", ")} #{should ? '' : 'not ' }to be called"
    msg << " #{@kind} #{@operation}" if @operation
    msg << " on #{@context}" if @context
    msg << ( @guess ? ", but got method #{@guess.filter} called" : ", but no callback found" )
    msg << " #{@guess.kind} #{@operation}" if @guess
    msg << " on another context" if @guess and !@context_match

    msg
  else
    "Callback#{@methods.size > 1 ? 's' : '' } #{@methods.join(", ")} can"\
    "not be tested against undefined lifecycle. Use .before, .after or .around"
  end
end

Private Instance Methods

check_context?(callback, context) click to toggle source
# File lib/matchers/callbacks.rb, line 91
def check_context?(callback, context)
  return true if !context

  options = callback.instance_variable_get(:@if)
  @context_match = options.select do |o|
    o.is_a?(Proc)
  end.detect do |o|
    o.call(ValidationContext.new(context))
  end
end
check_filter?(callback, method) click to toggle source
# File lib/matchers/callbacks.rb, line 83
def check_filter?(callback, method)
  callback.filter == method
end
check_kind?(callback, kind) click to toggle source
# File lib/matchers/callbacks.rb, line 87
def check_kind?(callback, kind)
  callback.kind == kind
end