Fix test passing blank input into required field

This commit is contained in:
2026-05-09 15:37:02 +02:00
parent 747a8a9357
commit 33eb2a8c79
2 changed files with 33 additions and 21 deletions

View File

@@ -44,10 +44,12 @@ class UnitsTest < ApplicationSystemTestCase
within 'tbody > tr:has(input[type=text], textarea)' do
assert_selector ':focus'
maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 2**16] }
maxlength = all(:fillable_field).to_h do |field|
[field[:name], field[:maxlength].to_i || 2**16]
end
values = {
symbol: random_string(rand([1..3, 4..maxlength['unit[symbol]']].sample),
except: units.map(&:symbol)),
symbol: random_string(deep_rand(1..3, 4..maxlength['unit[symbol]']),
except: units.map(&:symbol), allow_blank: false),
description: random_string(rand(0..maxlength['unit[description]']))
}.with_indifferent_access
within :field, 'unit[multiplier]' do |field|
@@ -61,7 +63,8 @@ class UnitsTest < ApplicationSystemTestCase
end
end
assert_selector '.flash.notice', text: t('units.create.success', unit: Unit.last.symbol)
assert_selector '.flash.notice',
text: t('units.create.success', unit: Unit.last.symbol)
within 'tbody' do
assert_no_selector :fillable_field
assert_selector 'tr', count: @user.units.count

View File

@@ -13,27 +13,36 @@ class ActiveSupport::TestCase
include ActionView::Helpers::TranslationHelper
# List of categorized Unicode characters:
# * http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
# File format: http://www.unicode.org/L2/L1999/UnicodeData.html
# Select from graphic ranges: L, M, N, P, S, Zs
UNICODE_CHARS = {
1 => [*"\u0020".."\u007E"],
2 => [*"\u00A0".."\u00AC",
# * source: http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
# * file format: http://www.unicode.org/L2/L1999/UnicodeData.html
# * select from graphic ranges: L, M, N, P, S, Zs
UNICODE_CHARS = [
*"\u0020".."\u007E",
*"\u00A0".."\u00AC",
*"\u00AE".."\u05FF",
*"\u0606".."\u061B",
*"\u061D".."\u06DC",
*"\u06DE".."\u070E",
*"\u0710".."\u07FF"]
}
UNICODE_CHARS.default = UNICODE_CHARS[1] + UNICODE_CHARS[2]
def random_string(bytes = 10, except: [])
*"\u0710".."\u07FF"
]
def random_string(length, except: [], allow_blank: true)
begin
result = ''
result += UNICODE_CHARS[bytes - result.bytesize].sample while bytes > result.bytesize
end while except.include?(result)
result = UNICODE_CHARS.sample(length).join
end while except.include?(result) || (!allow_blank && result.blank?)
result
end
def deep_rand(*args)
case args
when Array
args = args.sample
when Range
args = rand(args)
else
return args
end while true
end
# Assumes: max >= step and step = 1e[-]N, both as strings
def random_number(max, step)
max.delete!('.')