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

Already on GitHub? Sign in to your account

Shoulda failing in on valid input with ActiveModel #191

Closed
drsquidop opened this Issue Oct 1, 2011 · 3 comments

Comments

Projects
None yet
3 participants

Thank's for this useful gem!

I'm using shoulda (2.11.3), Rails 3.1.0 and Ruby 1.87

My RSpec model test is failing with what should be valid input to the model. Also, no detail is showing regarding the error object.

test_model.rb:

 class TestModel
    include ActiveModel::Validations
    extend ActiveModel::Naming 
    include ActiveModel::Conversion
    include ActiveModel::Serialization
    include ActiveModel::Serializers::Xml
  include ActiveModel::AttributeMethods    

  validates_presence_of :first_name
  validates :zip, :numericality => true

    def persisted?
      false
    end

    def initialize(attributes = {})
        @attributes = attributes
        Rails.logger.info self.inspect
    end

    #TODO in activerecord this is not needed, does new automatically generate nil for each field?
  attr_accessor :attributes, :first_name, :zip

    def read_attribute_for_validation(key)
        @attributes[key]
    end

    def save
    if valid?
      #save method implementation
      true
    else
      false
    end
  end
end

test_model_spec.rb:

require 'spec_helper'
#using gem shoulda, rspec-rails-matchers

describe TestModel do
  it_should_behave_like AnActiveModel

describe '#first_name' do
  it { should validate_presence_of(:first_name) }
  it { should_not allow_value("").for(:first_name) }
  it { should allow_value("First").for(:first_name) }
end

describe '#zip' do
  it { should_not allow_value("blah").for(:zip) }
  it { should allow_value("12345").for(:zip) }
end

end

Test result of running rspec spec/models/test_model_spec.rb:

Failures:

  1) TestModel#first_name 
     Failure/Error: it { should allow_value("First").for(:first_name) }
       Did not expect errors when first_name is set to "First", got error: 
     # ./spec/models/test_model_spec.rb:13

  2) TestModel#zip 
     Failure/Error: it { should allow_value("12345").for(:zip) }
       Did not expect errors when zip is set to "12345", got error: 
     # ./spec/models/test_model_spec.rb:18

Finished in 0.18562 seconds
25 examples, 2 failures

cscairns commented Jan 8, 2012

Got the same error.

Contributor

gabebw commented Mar 9, 2012

OK, confirmed that this is still an issue with shoulda-matchers 1.0.0. Here's a repo that shows the bug: https://github.com/gabebw/shoulda-issue-191

Contributor

gabebw commented Jul 13, 2012

When I removed the read_attribute_for_validation method, all tests passed in this demo repo: https://github.com/gabebw/shoulda-issue-191 . I think that this is a problem with ActiveModel not exactly mirroring ActiveRecord, or maybe having bad docs. Closing.

@gabebw gabebw closed this Jul 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment