summaryrefslogtreecommitdiff
path: root/spec/support/matchers
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-23 15:08:46 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-23 15:08:46 +0000
commit3f9e1b261121f4dbd045341241f81b47356c99cf (patch)
tree32be23bd7fda0c3f891182f220f6d0399a1b41dd /spec/support/matchers
parent5ad0cf26551baff8f08af8562a8d45e6ec14d71a (diff)
downloadgitlab-ce-3f9e1b261121f4dbd045341241f81b47356c99cf.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support/matchers')
-rw-r--r--spec/support/matchers/log_spam.rb53
1 files changed, 43 insertions, 10 deletions
diff --git a/spec/support/matchers/log_spam.rb b/spec/support/matchers/log_spam.rb
index f6aa7dbd152..260d2930816 100644
--- a/spec/support/matchers/log_spam.rb
+++ b/spec/support/matchers/log_spam.rb
@@ -1,29 +1,31 @@
# frozen_string_literal: true
# This matcher checks if one spam log with provided attributes was created
+# during the block evocation.
#
# Example:
#
-# expect { create_issue }.to log_spam
-RSpec::Matchers.define :log_spam do |expected|
- def spam_logs
- SpamLog.all
- end
+# expect { create_issue }.to log_spam(key1: value1, key2: value2)
+RSpec::Matchers.define :log_spam do |expected|
match do |block|
+ @existing_logs_count = SpamLog.count
+
block.call
- expect(spam_logs).to contain_exactly(
- have_attributes(expected)
- )
+ @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)
end
description do
- count = spam_logs.count
+ count = @new_logs_count - @existing_logs_count
if count == 1
keys = expected.keys.map(&:to_s)
- actual = spam_logs.first.attributes.slice(*keys)
+ actual = @last_spam_log.attributes.slice(*keys)
"create a spam log with #{expected} attributes. #{actual} created instead."
else
"create exactly 1 spam log with #{expected} attributes. #{count} spam logs created instead."
@@ -32,3 +34,34 @@ RSpec::Matchers.define :log_spam do |expected|
supports_block_expectations
end
+
+# 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