Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion about error message when using 'validate_numericality_of' #1490

Open
gruschis opened this issue Feb 21, 2022 · 1 comment
Open

Comments

@gruschis
Copy link

I have a class that includes a number of ActiveModel modules. I removed the (hopefully unnecessary) rest of the code for clarity.
Using RSpec and FactoryBot.

# app/models/evaluation.rb

class Evaluation
  include ActiveModel::Model
  include ActiveModel::AttributeAssignment
  include ActiveModel::Attributes
  include ActiveModel::Validations
  extend ActiveModel::Translation

  attribute :range_classification, :integer, default: 0

  validates! :range_classification,
             presence: true,
             numericality: {
               in: (-100..100),
               only_integer: true
             }
end
# spec/models/evaluation_spec.rb

RSpec.describe Evaluation do
  subject { build(described_class.name.underscore) }

  describe 'validations' do
    it { is_expected.to validate_presence_of(:range_classification).strict }

    it do
      is_expected.to(
        validate_numericality_of(:range_classification)
          .is_greater_than_or_equal_to(-100)
          .is_less_than_or_equal_to(100)
          .only_integer
          .strict
      )
    end
  end
end

The second example fails with ArgumentError: wrong number of arguments (given 1, expected 0).
After removing the qualifiers is_greater_than_or_equal_to and is_less_than_or_equal_to, the test passes.

Just for fun, I left in the qualifiers without passing any arguments. Assumable, the test fails because the qualifier actually expects an argument. The test fails with ArgumentError: wrong number of arguments (given 0, expected 1).

I'm a little confused where the error is rooted. Am I forgetting something?

LMK if you need more context.

@mcmire
Copy link
Collaborator

mcmire commented Mar 1, 2022

Hi @gruschis, I suspect this error is coming from deeper in the stack. Would you be willing to paste a backtrace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants