Skip to content

Commit eaba644

Browse files
committed
Add be_same_time_as matcher written by and thanks to @ManageIQ team
1 parent 6ceb4d4 commit eaba644

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

time_matchers.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Thanks to ManageIQ team for the be_same_time_as matcher, see
2+
# https://github.com/ManageIQ/manageiq/blob/f647d72ec6f8ba644d753fb041c8ecad277c3ef4/spec/support/custom_matchers/be_same_time_as.rb
3+
RSpec::Matchers.define :be_same_time_as do |expected|
4+
match do |actual|
5+
actual.round(precision) == expected.round(precision)
6+
end
7+
8+
failure_message do |actual|
9+
"\nexpected: #{format_time(expected)},\n " \
10+
"got: #{format_time(actual)}\n\n(compared using be_same_time_as with precision of #{precision})"
11+
end
12+
13+
failure_message_when_negated do
14+
"\nexpected different time from #{format_time(expected)}\n\n" \
15+
"(compared using be_same_time_as with precision of #{precision})"
16+
end
17+
18+
description do
19+
"be the same time as #{format_time(expected)} to #{precision} digits of precision"
20+
end
21+
22+
def with_precision(p)
23+
@precision = p
24+
self
25+
end
26+
27+
def precision
28+
@precision ||= 5
29+
end
30+
31+
private
32+
33+
def format_time(t)
34+
"#<#{t.class} #{t.iso8601(10)}>"
35+
end
36+
end

0 commit comments

Comments
 (0)