Files
openfoodnetwork/spec/validators/integer_array_validator_spec.rb
David Rodríguez 4c6d894bc0 Bump RuboCop to 1.86.6
There were a few changes needed:

* Plugins are now specified through `plugin:` config keyword.
* All plugin gems need to be specified explicitly in Gemfile since they
  are no longer dependencies of plugins already specified explicitly.
* All plugin gems need to be updated in other to use the new APIs.
* One cop was renamed.
* New offenses safe to correct were corrected directly with `bundle exec
  rubocop -a`.
* New offenses unsafe to correct were added to the TODO configuration
  with `bundle exec rubocop --auto-gen-config --auto-gen-only-exclude
  --exclude-limit 1400 --no-auto-gen-timestamp`.
2025-10-27 11:30:33 +01:00

60 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require "spec_helper"
RSpec.describe IntegerArrayValidator do
describe "internationalization" do
it "has translation for NOT_ARRAY_ERROR" do
expect(described_class.not_array_error).not_to be_blank
end
it "has translation for INVALID_ELEMENT_ERROR" do
expect(described_class.invalid_element_error).not_to be_blank
end
end
describe "validation" do
let(:instance) do
Class.new do
include ActiveModel::Validations
attr_accessor :ids
validates :ids, integer_array: true
end.new
end
it "does not add error when nil" do
instance.ids = nil
expect(instance).to be_valid
end
it "does not add error when blank array" do
instance.ids = []
expect(instance).to be_valid
end
it "adds error NOT_ARRAY_ERROR when neither nil nor an array" do
instance.ids = 1
expect(instance).not_to be_valid
expect(instance.errors[:ids]).to include(described_class.not_array_error)
end
it "does not add error when array of integers" do
instance.ids = [1, 2, 3]
expect(instance).to be_valid
end
it "does not add error when array of integers as String" do
instance.ids = ["1", "2", "3"]
expect(instance).to be_valid
end
it "adds error INVALID_ELEMENT_ERROR when an element cannot be parsed as Integer" do
instance.ids = [1, "2", "Not Integer", 3]
expect(instance).not_to be_valid
expect(instance.errors[:ids]).to include(described_class.invalid_element_error)
end
end
end