Module: BetterAssertDifference

Defined in:
lib/better_assert_difference.rb,
lib/better_assert_difference/version.rb,
lib/better_assert_difference/exceptions/difference_exception.rb

Defined Under Namespace

Classes: DifferenceException

Constant Summary collapse

DEFAULT_DIFF =
1
ACTIVE_RECORD_ENABLED =
!!defined?(ActiveRecord)
VERSION =
'0.1.14'

Instance Method Summary collapse

Instance Method Details

#assert_difference(expression, difference = DEFAULT_DIFF, message = nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/better_assert_difference.rb', line 8

def assert_difference(expression, difference = DEFAULT_DIFF, message = nil, &block)
  expressions = active_record?(expression) ? [expression] : expression
  expression_to_diff =
    if expressions.is_a?(Hash)
      expressions
    else
      Array(expressions).each_with_object({}) { |exp, expression_hash| expression_hash[exp] = difference }
    end

  block_to_diff =
    expression_to_diff.each_with_object({}) do |(exp, diff), expression_hash|
      raise DifferenceException.new(diff) unless diff.is_a?(Integer)
      key =
        if exp.respond_to?(:call)
          exp
        elsif ACTIVE_RECORD_ENABLED && active_record?(exp)
          -> { exp.all.reset.count }
        else
          -> { eval(exp, block.binding) }
        end
      expression_hash[key] = diff
    end
  before = block_to_diff.keys.map(&:call)
  retval = yield
  after = block_to_diff.keys.map(&:call)

  errors = []
  before.zip(after, expression_to_diff) do |before_value, after_value, (exp, diff)|
    next if before_value + diff == after_value
    error = "[#{expression_to_diff.find_index { |expression, _| expression == exp }}] " if expression_to_diff.size > 1
    name =
      if exp.is_a?(Class)
        exp.name
      elsif exp.class.name === 'ActiveRecord::Relation'
        exp.class.to_s
      else
        exp.inspect
      end
    error  = "#{error}#{name} didn't change by #{diff} {before: #{before_value}, after: #{after_value}}"
    error  = "#{message}.\n#{error}" if message
    errors << error
  end
  if errors.any?
    errors.unshift "#{errors.size} assertion#{errors.length > 1 ? 's' : ''} failed:"
    raise Minitest::Assertion, errors.join("\n")
  end

  retval
end