thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.5k stars 911 forks source link

RangeError: cannot get the first element of beginless range #1614

Closed pjpires closed 6 months ago

pjpires commented 6 months ago

Description

Currently, beginless and endless ranges are not correctly handled by .in_range on top of validates_inclusion_of, even though ActiveRecord properly handles beginless and endless ranges.

Reproduction Steps

Given a sample model:

# app/models/past_post.rb
class PastPost < ApplicationRecord
  validates :created_on, inclusion: { in: -> { (..Date.yesterday) } }
end

And the respective spec:

# spec/models/past_post_spec.rb

RSpec.describe PastPost do
  # ...

  describe 'Validations' do
    it { is_expected.to validate_inclusion_of(:created_on).in_range(..Date.yesterday) }
  end
end

An error is raised instead of correctly asserting the validation:

     Failure/Error: it { is_expected.to validate_inclusion_of(:created_on).in_range(..Date.yesterday) }

     RangeError:
       cannot get the first element of beginless range

A similar issue is raised for endless ranges:

     RangeError:
       cannot get the maximum of endless range

System configuration

shoulda_matchers version: 6.1.0 rails version: 7.1.3 ruby version: 3.2.2