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

Tests Fail : CouchRest::Model::Validations::Uniqueness #192

Open
pjfitzgibbons opened this issue Mar 22, 2015 · 3 comments
Open

Tests Fail : CouchRest::Model::Validations::Uniqueness #192

pjfitzgibbons opened this issue Mar 22, 2015 · 3 comments

Comments

@pjfitzgibbons
Copy link

HI Sam,
I'm receiving this during "rake spec". If you could provide any advice, I am prepping a pull request to update dependencies (mime_types gem) for Rails/ActiveModel 4.2.

I notice during debugging this, that during the test, UniquenessValidator#setup is never called.

RuntimeError: View WithUniqueValidation.by_title does not exist for validation!
./lib/couchrest/model/validations/uniqueness.rb:38:in `validate_each'
./lib/couchrest/model/validations.rb:31:in `valid?'
./lib/couchrest/model/persistence.rb:108:in `perform_validations'
./lib/couchrest/model/persistence.rb:10:in `create'
./lib/couchrest/model/persistence.rb:133:in `create'
./spec/unit/validations_spec.rb:9:in `block (5 levels) in <top (required)>'
./spec/unit/validations_spec.rb:9:in `map'
./spec/unit/validations_spec.rb:9:in `block (4 levels) in <top (required)>'

1 example, 1 failure, 0 passed
@samlown
Copy link
Member

samlown commented Mar 23, 2015

Hi! I suspect something has changed in ActiveModel 4.2 and validation. I've not tested against this version yet and we don't have any plans to upgrade just at the moment, but I'll take a look unless anyone else gets there first.

@aptos
Copy link

aptos commented Apr 7, 2015

I'm on 4.1 and have not upgraded to 4.2 yet because I began to get this deprecation warning:

DEPRECATION WARNING: The `Validator#setup` instance method is deprecated and will be removed     on Rails 4.2. Do your setup in the constructor instead:

class MyValidator < ActiveModel::Validator
  def initialize(options={})
    super
    options[:class].send :attr_accessor, :custom_attribute
  end
end

When I comment out uniqueness validators, the warning goes away. I have format and presence validations as well, but they don't cause this warning.

@aptos
Copy link

aptos commented Apr 7, 2015

The following change fixes the issue. I ran the unit/validations_spec.rb to be sure. By the way, what's the proper way to set username and password to run specs? I tried changing COUCHHOST in spec_helper but that didn't do it, so I temporarily hard coded configuration.rb to run the specs. In any event, here's the modification to validations/uniqueness.rb

  def initialize(options)
      super
      setup_ensure_view(options[:class]) if options[:class]
    end

    # Ensure we have a class available so we can check for a usable view
    # or add one if necessary.
    def setup_ensure_view(model)
      @model = model
      ....

The same pattern is found at: https://github.com/johncarney/validates_timeliness/blob/master/lib/validates_timeliness/validator.rb

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

3 participants