简体   繁体   中英

How do I pass a method with two arguments to a Minitest Spec assertion?

With Minitest Spec in Rails I'm trying to check if an ActiveSupport::TimeWithZone is in a certain range. I thought to use the between? method that takes the min and max of the range. Here's how I'm expressing that in Minitest Spec:

_(language_edit.curation_date).must_be :between?, 10.seconds.ago, Time.zone.now

but it gives me this error:

Minitest::UnexpectedError: ArgumentError: wrong number of arguments (1 for 2)

What am I doing wrong?

Looks like must_be is implemented as infect_an_assertion :assert_operator, :must_be

assert_operator

# File lib/minitest/unit.rb, line 299
def assert_operator o1, op, o2, msg = nil
  msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }
  assert o1.__send__(op, o2), msg
end

What if you use assert directly?

Example:

class DateTest < ActiveSupport::TestCase
  test "using assert with between? should work" do
    a = 5.seconds.ago

    assert a.between?(10.seconds.ago, Time.zone.now)
  end
end

Thanks to radubogdan for showing me some of the code behind the must_be method. It looks like it's designed to be used with operators like this:

_(language_edit.curation_date).must_be :>, 10.seconds.ago

and a side-effect of this is it works with boolean methods that take one or no arguments, but not with methods that take more than one argument. I think I'm supposed to do this:

_(language_edit.curation_date.between?(10.seconds.ago, Time.zone.now)).must_equal true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM