Class: Minitest::MustWonted::Matcher::Valid
- Defined in:
- lib/minitest/mustwonted/matcher/valid.rb
Overview
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
Instance Method Summary collapse
-
#initialize(name) ⇒ Valid
constructor
A new instance of Valid.
- #match?(subject, wont) ⇒ Boolean
- #test!(subject, wont) ⇒ Object
- #with(*args) ⇒ Object
Constructor Details
#initialize(name) ⇒ Valid
Returns a new instance of Valid.
11 12 13 14 |
# File 'lib/minitest/mustwonted/matcher/valid.rb', line 11 def initialize(name) @name = name.to_sym @args = [] end |
Instance Method Details
#match?(subject, wont) ⇒ Boolean
16 17 18 19 20 21 22 23 24 25 |
# 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) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
27 28 29 30 31 |
# File 'lib/minitest/mustwonted/matcher/valid.rb', line 27 def with(*args) @args = args self end |