gitlab-org--gitlab-foss/spec/support/matchers.rb

67 lines
1.4 KiB
Ruby
Raw Normal View History

2011-10-08 21:36:38 +00:00
RSpec::Matchers.define :be_valid_commit do
match do |actual|
2015-05-23 21:32:39 +00:00
actual &&
actual.id == ValidCommit::ID &&
actual.message == ValidCommit::MESSAGE &&
actual.author_name == ValidCommit::AUTHOR_FULL_NAME
2011-10-08 21:36:38 +00:00
end
end
2015-05-23 21:32:39 +00:00
def emulate_user(user)
user = case user
when :user then create(:user)
when :visitor then nil
when :admin then create(:admin)
else user
end
login_with(user) if user
end
2011-10-08 21:36:38 +00:00
RSpec::Matchers.define :be_allowed_for do |user|
match do |url|
2015-05-23 21:32:39 +00:00
emulate_user(user)
visit url
status_code != 404 && current_path != new_user_session_path
2011-10-08 21:36:38 +00:00
end
end
RSpec::Matchers.define :be_denied_for do |user|
match do |url|
2015-05-23 21:32:39 +00:00
emulate_user(user)
visit url
status_code == 404 || current_path == new_user_session_path
end
2011-10-08 21:36:38 +00:00
end
2015-05-23 21:32:39 +00:00
RSpec::Matchers.define :be_not_found_for do |user|
2011-10-17 10:39:03 +00:00
match do |url|
2015-05-23 21:32:39 +00:00
emulate_user(user)
visit url
status_code == 404
end
2011-10-17 10:39:03 +00:00
end
2012-08-29 05:49:26 +00:00
RSpec::Matchers.define :include_module do |expected|
match do
described_class.included_modules.include?(expected)
end
2015-03-28 02:49:57 +00:00
description do
2015-05-23 21:32:39 +00:00
"includes the #{expected} module"
2015-03-28 02:49:57 +00:00
end
failure_message do
2012-08-29 05:49:26 +00:00
"expected #{described_class} to include the #{expected} module"
end
end
# Extend shoulda-matchers
module Shoulda::Matchers::ActiveModel
2015-05-18 20:40:10 +00:00
class ValidateLengthOfMatcher
# Shortcut for is_at_least and is_at_most
def is_within(range)
2012-09-19 16:14:42 +00:00
is_at_least(range.min) && is_at_most(range.max)
end
end
end