2019-09-24 23:06:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
# This matcher checks if one spam log with provided attributes was created
|
2020-01-23 10:08:46 -05:00
|
|
|
# during the block evocation.
|
2019-09-24 23:06:21 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2020-01-23 10:08:46 -05:00
|
|
|
# expect { create_issue }.to log_spam(key1: value1, key2: value2)
|
2019-09-24 23:06:21 -04:00
|
|
|
|
2020-01-23 10:08:46 -05:00
|
|
|
RSpec::Matchers.define :log_spam do |expected|
|
2019-09-24 23:06:21 -04:00
|
|
|
match do |block|
|
2020-01-23 10:08:46 -05:00
|
|
|
@existing_logs_count = SpamLog.count
|
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
block.call
|
|
|
|
|
2020-01-23 10:08:46 -05:00
|
|
|
@new_logs_count = SpamLog.count
|
|
|
|
@last_spam_log = SpamLog.last
|
|
|
|
|
|
|
|
expect(@new_logs_count - @existing_logs_count).to eq 1
|
|
|
|
expect(@last_spam_log).to have_attributes(expected)
|
2019-09-24 23:06:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
description do
|
2020-01-23 10:08:46 -05:00
|
|
|
count = @new_logs_count - @existing_logs_count
|
2019-09-24 23:06:21 -04:00
|
|
|
|
|
|
|
if count == 1
|
|
|
|
keys = expected.keys.map(&:to_s)
|
2020-01-23 10:08:46 -05:00
|
|
|
actual = @last_spam_log.attributes.slice(*keys)
|
2019-09-24 23:06:21 -04:00
|
|
|
"create a spam log with #{expected} attributes. #{actual} created instead."
|
|
|
|
else
|
|
|
|
"create exactly 1 spam log with #{expected} attributes. #{count} spam logs created instead."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
supports_block_expectations
|
|
|
|
end
|
2020-01-23 10:08:46 -05:00
|
|
|
|
|
|
|
# This matcher checks that the last spam log
|
|
|
|
# has the attributes provided.
|
|
|
|
# The spam log does not have to be created during the block evocation.
|
|
|
|
# The number of total spam logs just has to be more than one.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# expect { create_issue }.to have_spam_log(key1: value1, key2: value2)
|
|
|
|
|
|
|
|
RSpec::Matchers.define :have_spam_log do |expected|
|
|
|
|
match do |block|
|
|
|
|
block.call
|
|
|
|
|
|
|
|
@total_logs_count = SpamLog.count
|
|
|
|
@latest_spam_log = SpamLog.last
|
|
|
|
expect(SpamLog.last).to have_attributes(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
description do
|
|
|
|
if @total_logs_count > 0
|
|
|
|
keys = expected.keys.map(&:to_s)
|
|
|
|
actual = @latest_spam_log.attributes.slice(*keys)
|
|
|
|
"the last spam log to have #{expected} attributes. Last spam log has #{actual} attributes instead."
|
|
|
|
else
|
|
|
|
"there to be a spam log, but there are no spam logs."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
supports_block_expectations
|
|
|
|
end
|