forked from eliotsykes/rails-testing-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_matchers.rb
36 lines (29 loc) · 1001 Bytes
/
time_matchers.rb
1
2
3
4
5
6
7
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
# Thanks to ManageIQ team for the be_same_time_as matcher, see
# https://github.com/ManageIQ/manageiq/blob/f647d72ec6f8ba644d753fb041c8ecad277c3ef4/spec/support/custom_matchers/be_same_time_as.rb
RSpec::Matchers.define :be_same_time_as do |expected|
match do |actual|
actual.round(precision) == expected.round(precision)
end
failure_message do |actual|
"\nexpected: #{format_time(expected)},\n " \
"got: #{format_time(actual)}\n\n(compared using be_same_time_as with precision of #{precision})"
end
failure_message_when_negated do
"\nexpected different time from #{format_time(expected)}\n\n" \
"(compared using be_same_time_as with precision of #{precision})"
end
description do
"be the same time as #{format_time(expected)} to #{precision} digits of precision"
end
def with_precision(p)
@precision = p
self
end
def precision
@precision ||= 5
end
private
def format_time(t)
"#<#{t.class} #{t.iso8601(10)}>"
end
end