thoughtbot / shoulda-matchers

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

validate_numericality_of matcher trying to set a string value in 3.0 #784

Closed dillonwelch closed 8 years ago

dillonwelch commented 9 years ago

I'm trying to upgrade to version 3.0 and am running into this issue in one of my model specs. The validate_numericality_of matcher is trying to set a float field to a string, and erroring it out when that doesn't work. I would think that it would be expected for that assignment to fail. :confused:

# spec/rails_helper.rb
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

# migration file
t.float :hours_worked

# model file
validates :hours_worked, numericality: true

# spec file
it { expect(subject).to validate_numericality_of(:hours_worked) }

# error
Failure/Error: it { expect(subject).to validate_numericality_of(:hours_worked) }
Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
  Expected Class to be able to set hours_worked to "abcd", but got 0.0 instead.
chris-obv commented 9 years ago

We're receiving the same error.

brunoocasali commented 9 years ago

:+1:

mcmire commented 9 years ago

This was a conscious decision to ensure that people are writing tests that are true and not misleading.

validate_numericality_of makes the assumption that if your attribute is set to a non-numeric value, then it will cause your model to be invalid. But this isn't actually happening: since your column is numeric itself, it will typecast any value to a numeric (in this case a float). So your validation won't ever fail, because all values are now always numeric.

The exception you're getting is low-level and obviously we should make some improvements to the message, but basically, it's indicating that since the validation always passes, you can remove it and the test itself. Does this make sense?

dillonwelch commented 9 years ago

Yes, that makes sense now that you've explained it. Thanks! Definitely agree on seeing an improved message :+1:

sunderwood commented 9 years ago

I use validate_numericality_of to validate that the number is within a certain range of values (given that I know the field is numeric). Using validate_numericality_of provides better default error messages and options than validate_inclusion_of.

I can understand the desire to ensure that people are not adding superfluous validations to the model. Your test, and error are fine if someone does not include any of the options for the validates_numericality_of. Perhaps the test should warn that the field is already numeric instead of failing the test.

In the example above, if the validation was

validates :hours_worked, numericality: {greater_than: 0}

I would expect

it { expect(subject).to validate_numericality_of(:hours_worked).is_greater_than(0)}

to pass, but it doesn't.

cofiem commented 9 years ago

I understand and appreciate the rationale behind setting a numerical field to a string as a test. Similar to what @sunderwood says, I can not work out how to get Rails' built-in numericality validation to actually pass the validate_numericality_of test.

For example, I would expect this test to pass, but it does not:

# validation
validates :channels, presence: true, numericality: {only_integer: true, greater_than: 0}

# tests
it { is_expected.to validate_presence_of(:channels) }
it { is_expected.to validate_numericality_of(:channels).is_greater_than(0).only_integer }

# error
validation should only allow integers for channels which are greater than 0
     Failure/Error: it { is_expected.to validate_numericality_of(:channels).is_greater_than(0).only_integer }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       Expected Class to be able to set channels to "abcd", but got 0 instead.

Can you clarify how this is intended to work? Should the validate_numericality_of pass using Rails built-in numericality validation?

celsoMartins commented 9 years ago

I'm getting this error too.

And so, if I understood well, I'll need to get rid of shoulda and I need to use pure rspec to test two words (greather_than) on my model.

My new specs:

    context 'with zero values' do
      it 'refuses below zero values' do
        workforce.amount_centavos = 0
        expect(workforce).not_to be_valid
      end
    end

    context 'with negative values' do
      it 'refuses below zero values' do
        workforce.amount_centavos = -1
        expect(workforce).not_to be_valid
      end
    end

instead of

it { expect(workforce).to validate_numericality_of(:amount_centavos).is_greater_than(0) }

Am I right? If it is correct, I choose to keep the 2.8 version until we have an option or shoulda reconsider it.

mcmire commented 9 years ago

Yes, the bug where the numericality matcher raises an error when using comparison qualifiers (greater_than, less_than, etc.) is a known issue (raised in another issue recently) and I'll put out a fix for that soon. I'd stay at 2.8.0 until that's fixed.

celsoMartins commented 9 years ago

thanks.

QuotableWater7 commented 8 years ago

:+1:

arronmabrey commented 8 years ago

I ran into this issue as well, but before looking for it in github issues I made a small focused reproduction script. I figure I will add it here if it helps anyone.

#!/usr/bin/env ruby

begin
  require 'bundler/inline'
rescue LoadError => e
  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true) do
  source 'https://rubygems.org'

  gem 'sqlite3'
  gem 'activerecord', '4.2.4'
  gem 'rspec', '3.3.0'
  gem 'shoulda-matchers', '3.0.0'
end

require 'active_record'
require 'rspec/autorun'
require 'logger'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :active_record
    with.library :active_model
  end
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts do |t|
    t.integer :comment_count
  end
end

class Post < ActiveRecord::Base
  validates_numericality_of :comment_count, only_integer: true, greater_than_or_equal_to: 0, allow_nil: true
end

RSpec.describe Post, type: :model do
  it { should validate_numericality_of(:comment_count).only_integer.is_greater_than_or_equal_to(0).allow_nil }
end

Running the above script fails with:

Failures:

  1) Post should only allow integers for comment_count which are greater than or equal to 0
     Failure/Error: it { should validate_numericality_of(:comment_count).only_integer.is_greater_than_or_equal_to(0).allow_nil }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       Expected Class to be able to set comment_count to "abcd", but got 0 instead.
     # ./shoulda-matchers-fail.rb:55:in `block (2 levels) in <main>'

Finished in 0.00613 seconds (files took 0.44861 seconds to load)
1 example, 1 failure

Failed examples:

rspec  # Post should only allow integers for comment_count which are greater than or equal to 0
mcmire commented 8 years ago

Yup, I'm working on this :smiley:

arronmabrey commented 8 years ago

@mcmire thanks so much :tada: :balloon:

mcmire commented 8 years ago

This is fixed in 18b2859d2522a4866c398b9a32ebc3de4ec79389.

zerocool4u2 commented 8 years ago

Hi, really appreciate your work, I'm having a couple of troubles like this, specifically with decimals attributes an integers, with decimals I'm getting "abcd" type casted to BigDecimals(0.0) in any type of range limit and with the integer typecast floats to integers(0.1 to 0) when check only_integer, i get the message for submitting an issue, but it's looks like that is the same problem of typecast(the commit doesn't fix my cases), so I'm guessing that the current solution its patch up cases, should i still submit the issues? regards! and again, really appreciated man, take care

mcmire commented 8 years ago

@zerocool4u2 Sorry about that. What you're seeing may be fixed by the commit I just made. Can you try pointing to master temporarily and seeing if that resolves the issues?

zerocool4u2 commented 8 years ago

@mcmire nope, there are still there, here are the failures I'm getting

Bundle
Using shoulda-matchers 3.0.0 from git://github.com/thoughtbot/shoulda-matchers.git (at master)

Decimal

#migration
t.decimal :lat

#model
validates :lat, presence: true, numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90 }

#spec
it { expect(address).to validate_numericality_of(:lat).is_greater_than_or_equal_to(-90).is_less_than_or_equal_to(90) }

#error
  1) Address ActiveModel Validations should only allow numbers for lat which are greater than or equal to -90 and less than or equal to 90
     Failure/Error: it { expect(address).to validate_numericality_of(:lat).is_greater_than_or_equal_to(-90).is_less_than_or_equal_to(90) }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       The allow_value matcher attempted to set :lat on Address to "abcd", but
       when the attribute was read back, it had stored
       #<BigDecimal:67ea2d8,'0.0',9(9)> instead.

       This creates a problem because it means that the model is behaving in a
       way that is interfering with the test -- there's a mismatch between the
       test that was written and test that was actually run.

       There are a couple of reasons why this could be happening:

       * The writer method for :lat has been overridden and contains custom
         logic to prevent certain values from being set or change which values
         are stored.
       * ActiveRecord is typecasting the incoming value.

       Regardless, the fact you're seeing this message usually indicates a
       larger problem. Please file an issue on the GitHub repo for
       shoulda-matchers, including details about your model and the test
       you've written, and we can point you in the right direction:

       https://github.com/thoughtbot/shoulda-matchers/issues
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:462:in `ensure_that_attribute_was_set!'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:454:in `set_attribute'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:448:in `value_matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `block in matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `each'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `all?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:16:in `matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:482:in `block in first_failing_submatcher'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `each'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `detect'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `first_failing_submatcher'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:408:in `matches?'
     # ./spec/models/address_spec.rb:43:in `block (3 levels) in <top (required)>'

Integer

#migration
t.integer :number

#model
validates :number, presence: true, numericality: { only_integer: true }

#spec
it { expect(region).to validate_numericality_of(:number).only_integer }

#error
  1) Region ActiveModel Validations should only allow integers for number
     Failure/Error: it { expect(region).to validate_numericality_of(:number).only_integer }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       The allow_value matcher attempted to set :number on Region to 0.1, but
       when the attribute was read back, it had stored 0 instead.

       This creates a problem because it means that the model is behaving in a
       way that is interfering with the test -- there's a mismatch between the
       test that was written and test that was actually run.

       There are a couple of reasons why this could be happening:

       * The writer method for :number has been overridden and contains custom
         logic to prevent certain values from being set or change which values
         are stored.
       * ActiveRecord is typecasting the incoming value.

       Regardless, the fact you're seeing this message usually indicates a
       larger problem. Please file an issue on the GitHub repo for
       shoulda-matchers, including details about your model and the test
       you've written, and we can point you in the right direction:

       https://github.com/thoughtbot/shoulda-matchers/issues
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:462:in `ensure_that_attribute_was_set!'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:454:in `set_attribute'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:448:in `value_matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `block in matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `each'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `all?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:16:in `matches?'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:482:in `block in first_failing_submatcher'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `each'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `detect'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `first_failing_submatcher'
     # /home/zerocool4u2/.rvm/gems/ruby-2.2.0/bundler/gems/shoulda-matchers-369e9d4c36cf/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:408:in `matches?'
     # ./spec/models/region_spec.rb:32:in `block (3 levels) in <top (required)>'

I hope you find them useful, regards

zerocool4u2 commented 8 years ago

Tracking the file shoulda-matchers/lib/shoulda/matchers/active_model/allow_value_matcher.rb i find out that until the commit 73cf275d104f62b32eb80b4e72577bd56ebdd730 all my test works, they break at 9d9dc4e6b9cf2c19df66a1b4ba432ad8d3e5dded, regards

wellsonjain commented 8 years ago

@zerocool4u2 I have the same problem as well. thank you for reporting this issue, I got stuck on this problem for a few days. @mcmire I've set the version to be the specific commit 18b2859d2522a4866c398b9a32ebc3de4ec79389 and I still got error, Here is my model, rspec and error msg.

# model
validates :price, numericality: { greater_than_or_equal_to: 0 }, presence: true

# rspec
it { should validate_numericality_of(:price).is_greater_than_or_equal_to(0) }

#error 
Failure/Error: it { should validate_numericality_of(:price).is_greater_than_or_equal_to(0) }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       The allow_value matcher attempted to set :price on Product to "abcd",
       but when the attribute was read back, it had stored
       #<BigDecimal:7fca4f901c78,'0.0',9(9)> instead.

       This creates a problem because it means that the model is behaving in a
       way that is interfering with the test -- there's a mismatch between the
       test that was written and test that was actually run.

       There are a couple of reasons why this could be happening:

       * The writer method for :price has been overridden and contains custom
         logic to prevent certain values from being set or change which values
         are stored.
       * ActiveRecord is typecasting the incoming value.

       Regardless, the fact you're seeing this message usually indicates a
       larger problem. Please file an issue on the GitHub repo for
       shoulda-matchers, including details about your model and the test
       you've written, and we can point you in the right direction:

       https://github.com/thoughtbot/shoulda-matchers/issues
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:462:in `ensure_that_attribute_was_set!'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:454:in `set_attribute'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:448:in `value_matches?'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `block in matches?'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `each'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `all?'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/allow_value_matcher.rb:406:in `matches?'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:16:in `matches?'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:482:in `block in first_failing_submatcher'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `each'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `detect'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:481:in `first_failing_submatcher'
     # /Users/wellsonjain/.rvm/gems/ruby-2.2.3@market-api/bundler/gems/shoulda-matchers-18b2859d2522/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb:408:in `matches?'
     # ./spec/models/product_spec.rb:14:in `block (2 levels) in <top (required)>'
mcmire commented 8 years ago

@zerocool4u2

In the case of the decimal, that's a bug -- I'm taking floats and integers into account but I forgot decimal.

In the case of the integer column, you don't actually need the validation or test at all -- an integer column already typecasts any incoming value to an integer, so there's no need for the validation, as it will always pass.

mcmire commented 8 years ago

@wellsonjain Same problem as @zerocool4u2 -- I'm guessing that that column is a decimal, and I forgot about that one. I'll work on that next.

graygilmore commented 8 years ago

I'm getting a similar error:

product.rb

class Product < ActiveRecord::Base
  validates :price, numericality: {
    greater_than_or_equal_to: 0,
    only_integer: true
  }
end

product_spec.rb

require "rails_helper"

RSpec.describe Product, type: :model do
  it do
    should validate_numericality_of(:price).
      is_greater_than_or_equal_to(0).
      only_integer
  end
end

Failure

Failures:

  1) Product should only allow integers for price which are greater than or equal to 0
     Failure/Error: should validate_numericality_of(:price).
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       The allow_value matcher attempted to set :price on Product to 0.1, but
       when the attribute was read back, it had stored 0 instead.

       This creates a problem because it means that the model is behaving in a
       way that is interfering with the test -- there's a mismatch between the
       test that was written and test that was actually run.

       There are a couple of reasons why this could be happening:

       * The writer method for :price has been overridden and contains custom
         logic to prevent certain values from being set or change which values
         are stored.
       * ActiveRecord is typecasting the incoming value.

       Regardless, the fact you're seeing this message usually indicates a
       larger problem. Please file an issue on the GitHub repo for
       shoulda-matchers, including details about your model and the test
       you've written, and we can point you in the right direction:

       https://github.com/thoughtbot/shoulda-matchers/issues
     # ./spec/models/product_spec.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.04772 seconds (files took 1.29 seconds to load)
5 examples, 1 failure

Failed examples:

rspec ./spec/models/product_spec.rb:6 # Product should only allow integers for price which are greater than or equal to 0

Looks like it's trying to set the value to 0.1 which is not an integer.

joshteng commented 8 years ago

I'm getting an unexpected error message.

I have in my model

validates :value, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

and this as my spec

it { should validate_numericality_of(:value).only_integer }

I would expect this to pass but I'm getting an error message:

     Failure/Error: it { should validate_numericality_of(:value).only_integer }
     Shoulda::Matchers::ActiveModel::AllowValueMatcher::CouldNotSetAttributeError:
       The allow_value matcher attempted to set :value on KarmaPoint to 0.1,
       but when the attribute was read back, it had stored 0 instead.

       This creates a problem because it means that the model is behaving in a
       way that is interfering with the test -- there's a mismatch between the
       test that was written and test that was actually run.

       There are a couple of reasons why this could be happening:

       * The writer method for :value has been overridden and contains custom
         logic to prevent certain values from being set or change which values
         are stored.
       * ActiveRecord is typecasting the incoming value.

       Regardless, the fact you're seeing this message usually indicates a
       larger problem. Please file an issue on the GitHub repo for
       shoulda-matchers, including details about your model and the test
       you've written, and we can point you in the right direction:

       https://github.com/thoughtbot/shoulda-matchers/issues

It says it tried to set the value to 0.1 but it had stored 0. That's correct because I'm restricting to integer. Why is that a false validation? Just checking to see if I'm missing something.

I'm using shoulda-matchers 3.0.1.

betesh commented 8 years ago

Because when you set it to 0.1, ActiveRecord will automatically cast it as an integer, i.e. 0.1.to_i, which is 0, so it's impossible to set an invalid value.

You do need to test the greater_than_or_equal_to part and you can use validate_numericality_of(:value) for that. Only the only_integer is unnecessary, so you can remove it from both your model and your spec.

joshteng commented 8 years ago

Thx @betesh

It makes sense, but wouldn't that be making assumptions about my database's data type? I know it probably doesn't make sense to hv integer validation and have the database data type to be a float/decimal. What if I wanted to assert integer validation in situations like these?

betesh commented 8 years ago

shoulda-matchers detects the data type. If you change your database column to a float, this test will start passing. But you're better off relying on built-in ActiveRecord features to keep invalid data out of your database than writing additional validations.

@mcmire's comment below explains this better. It's not shoulda-matchers that's detecting the type. It's ActiveRecord's attribute setters.

mcmire commented 8 years ago

@joshteng No assumption is being made about your database's data type here. Because you qualified the matcher with only_integer, it's going to perform its subtests using floats instead of integers (which is what it normally uses). The idea is that since you're saying that the values this attribute is storing must be integers, if floats are used instead, then the record should fail validation. However, this is impossible to test when the column itself is an integer, because the float the matcher uses (in this case 0.1) will get changed into an integer (0) when it's assigned to the attribute. Hence, this invalidates the test and the matcher has no choice but to abort.

As @betesh is saying, it turns out that if you're using an integer column, validating that the attribute disallows non-integers is redundant, since the column type already guarantees that the attribute is an integer. It can't be anything but, since ActiveRecord casts it to one. (It used to not be this way but the behavior was changed in Rails 4.2.) This actually isn't true -- the validation should still work regardless of the new typecasting behavior in Rails 4.2. So this is a bug.

ehannes commented 8 years ago

Any news on this?

mcmire commented 8 years ago

@ehannes I'm working on fixing the additional issues with the numericality matcher right now, it's a little slow going but I am working on it. Sorry I don't have anything else.

betesh commented 8 years ago

@mcmire What was incorrect about your original comment?

Xosmond commented 7 years ago

I have the decimal issue. Any news?

When assign "asb" decimal go "0.0" and also when assign "nil" decimal go "0.0" so i dont know how to validate or test this.

After setting :total to ‹nil› -- which was read back as ‹#<BigDecimal:7fe9f2540658,'0.0',9(18)>› -- the matcher expected the Purchase to be invalid and to produce the validation error "no puede estar en blanco" on :total.

After setting :total to ‹"abcd"› -- which was read back as ‹#<BigDecimal:7fe9f5977c00,'0.0',9(9)>› -- the matcher expected the Purchase to be invalid and to produce the validation error "no es un número" on :total

CaioBianchi commented 6 years ago

I'm having the same issue simply testing for numericality and only_integer

Model: validates :size, numericality: { integer_only: true }

Spec: it { should validate_numericality_of(:size).only_integer }

Result:

       Example did not properly validate that :size looks like an integer.
         After setting :size to ‹0.1› -- which was read back as ‹0› -- the
         matcher expected the Example to be invalid, but it was valid instead.

The issue seems to lie on the only_integer method.

mcmire commented 6 years ago

@CaioBianchi Would you mind creating a new issue for this? I don't want to keep adding things to this issue since it's closed.

ghost commented 3 years ago

any update?

mcmire commented 3 years ago

@mohsen-alizadeh-clark2 The original issue was fixed in https://github.com/thoughtbot/shoulda-matchers/commit/18b2859d2522a4866c398b9a32ebc3de4ec79389 and https://github.com/thoughtbot/shoulda-matchers/commit/a0f12216fd12627c2545b082f5e463a712ba75e5. If you are still having a problem, please make a new issue with an example of your database schema, code, and tests so I can track it better!