Class: Minitest::MustWonted::Matcher::Have

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/mustwonted/matcher/have.rb

Overview

The unversal ‘have` matcher

@user.must have(10).comments
@array.must have(2).items

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Have

Returns a new instance of Have.



9
10
11
# File 'lib/minitest/mustwonted/matcher/have.rb', line 9

def initialize(size)
  @size = size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/minitest/mustwonted/matcher/have.rb', line 26

def method_missing(name, *args)
  name = name.pluralize if name.respond_to?(:pluralize)

  @name = name.to_sym
  @args = args

  self # returning itself so the end result was always this matcher
end

Instance Method Details

#match?(subject, wont) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/minitest/mustwonted/matcher/have.rb', line 13

def match?(subject, wont)
  if [:items, :item].include?(@name) && !subject.respond_to?(@name)
    items = subject
  else
    items = subject.send @name, *@args
  end

  if wont ? items.size == @size : items.size != @size
    raise Minitest::Assertion, "Expected #{subject.inspect} to have #{
      @size} ##{@name}(#{@args.join(',')})\nbut instead it has: #{size}"
  end
end