class Minitest::MustWonted::Matcher::Valid

Generic validations matcher

user.must have_valid(:name)
user.must have_valid(:name).with('Nikolay', 'Vasilisa', ...)
user.wont have_valid(:name).with(nil, '', false)

Credits: we’re mimicking the minitest-matchers validation matchers here

Public Class Methods

new(name) click to toggle source
# File lib/minitest/mustwonted/matcher/valid.rb, line 11
def initialize(name)
  @name = name.to_sym
  @args = []
end

Public Instance Methods

match?(subject, wont) click to toggle source
# File lib/minitest/mustwonted/matcher/valid.rb, line 16
def match?(subject, wont)
  if @args.size == 0
    test!(subject, wont)
  else
    @args.each do |value|
      subject.send "#{@name}=", value
      test!(subject, wont)
    end
  end
end
test!(subject, wont) click to toggle source
# File lib/minitest/mustwonted/matcher/valid.rb, line 33
def test!(subject, wont)
  valid = subject.valid?
  error = subject.errors[@name]

  if error && !wont
    raise Minitest::Assertion, "Expected the #{
      subject.inspect} to have valid #{@name
      } with #{subject.send(@name).inspect
      }\nbut had an error instead: #{error.inspect}"
  elsif wont && !error
    raise Minitest::Assertion, "Expected the #{
      subject.inspect} to have invalid #{@name
      } with #{subject.send(@name).inspect
      }\nbut had no errors for this field instead"
  end
end
with(*args) click to toggle source
# File lib/minitest/mustwonted/matcher/valid.rb, line 27
def with(*args)
  @args = args

  self
end