我正在使用和gems 为我的表单(使用Reform
gem)设置规范。我无法弄清楚为什么我会遇到验证问题。 RSpec
Shoulda Matchers
我的实际配置:
ruby '2.5.1'
gem 'rails', '~> 5.2.0'
gem 'rspec-rails', '~> 3.8'
gem 'shoulda-matchers', '~> 4.0'
gem 'reform-rails', '~> 0.1'
我已经在我的表单中尝试了不同类型的长度验证。但没有什么能让事情变得更好。
这是用于测试的最小形式:
# frozen_string_literal: true
class UserForm < Reform::Form
# == Properties ==========================================================
property :nickname
# == Validations ==========================================================
validates :nickname, presence: true, length: { minimum: 3, maximum: 100 }
end
这是规格:
# frozen_string_literal: true
RSpec.describe UserForm, type: :model do
subject { described_class.new(User.new) }
it { is_expected.to validate_presence_of :nickname }
it { is_expected.to validate_length_of(:nickname).is_at_least(3).is_at_most(100) }
end
请注意,validate_presence_of
匹配器完美运行。
我有作为RSpec
输出:
1) UserForm should validate that the length of :nickname is between 3 and 100
Failure/Error: it { is_expected.to validate_length_of(:nickname).is_at_least(3).is_at_most(100) }
Expected UserForm to validate that the length of :nickname is between
3 and 100, but this could not be proved.
After setting :nickname to ‹"xxx"›, the matcher expected the
UserForm to be valid, but it was invalid instead, producing these
validation errors:
* nickname: ["is too short (at least 3 characters)"]
我显然不打算让这些验证工作。
我希望我能在这里找到帮助:)