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

validate_presence_of throws wrong number of arguments error on Class inherit from ActiveResource::Base #348

Closed
cseydel opened this issue Aug 27, 2013 · 7 comments

Comments

@cseydel
Copy link

cseydel commented Aug 27, 2013

Hi,
got an error with version 2.3.0 that hasn't occured in previous versions:

class Klass < ActiveResource::Base
    extend  ActiveModel::Callbacks
    include ActiveModel::MassAssignmentSecurity
    include ActiveModel::Validations::Callbacks

    validates :arg1, :arg2, :presence => true
    # ...
end

tested with

describe Klass do
    it { should validate_presence_of(:arg1) }
    it { should validate_presence_of(:arg2) }
end

throws with shoulda 3.5.0 and shoulda-matchers 2.3.0 following error:

Failure/Error: it { should validate_presence_of(:arg1) }
     ArgumentError:
       wrong number of arguments (1 for 0)
Failure/Error: it { should validate_presence_of(:arg2) }
     ArgumentError:
       wrong number of arguments (1 for 0)

Are there any suggestions or things I've been missing?
Best Christoph

@mcmire
Copy link
Collaborator

mcmire commented Aug 28, 2013

What's the full backtrace?

@cseydel
Copy link
Author

cseydel commented Aug 28, 2013

I'm sorry but there is no other backtrace at all and the test logs only are showing a number of commit and rollback commands resulting from my tests.

UPDATE:
Sorry, found the -b command of rspec :)

# /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/activeresource-3.2.13/lib/active_resource/validations.rb:123:in `valid?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validation_message_finder.rb:62:in `validate_instance'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validation_message_finder.rb:58:in `validated_instance'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validation_message_finder.rb:54:in `errors'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validation_message_finder.rb:24:in `has_messages?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:93:in `has_messages?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:89:in `errors_match?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:67:in `block in matches?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:64:in `each'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:64:in `none?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:64:in `matches?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:10:in `matches?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validation_matcher.rb:48:in `disallows_value_of'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/shoulda-matchers-2.3.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:32:in `matches?'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-expectations-2.12.1/lib/rspec/expectations/handler.rb:23:in `handle_matcher'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/subject.rb:64:in `should'
     # ./spec/lib/project/klass_spec.rb:39:in `block (3 levels) in <top (required)>'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `instance_eval'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `block in run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
     # /Users/christophseydel/.rvm/gems/ruby-1.9.3-p374@project/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'

@cseydel
Copy link
Author

cseydel commented Aug 28, 2013

I looked at the tags and it looks like that it is broken since v2.1.0. At this point the validate_instance method in lib/shoulda/matchers/active_model/validation_message_finder.rb was edited to pass a context param.

v2.0.0

def validate_instance
    @instance.valid?
    @instance
end

v2.1.0

def validate_instance
    @instance.valid?(@context)
    @instance
end

carpodaster added a commit to carpodaster/shoulda-matchers that referenced this issue Aug 28, 2013
As reported in thoughtbot#348, model#valid? is called with one argument while
ActiveResource::Validations only defines #valid? without an optional
argument.

The interface of ActiveResouce::Validations#valid? is the same for
A'resource in Rails 3.2 and the one extracted into a gem.

Rails 3.2 version:
https://github.com/rails/rails/blob/3-2-stable/activeresource/lib/active_resource/validations.rb#L123

Gem version:
https://github.com/rails/activeresource/blob/master/lib/active_resource/validations.rb#L160
mcmire pushed a commit that referenced this issue Sep 11, 2013
As reported in #348, model#valid? is called with one argument while
ActiveResource::Validations only defines #valid? without an optional
argument.

The interface of ActiveResouce::Validations#valid? is the same for
A'resource in Rails 3.2 and the one extracted into a gem.

Rails 3.2 version:
https://github.com/rails/rails/blob/3-2-stable/activeresource/lib/active_resource/validations.rb#L123

Gem version:
https://github.com/rails/activeresource/blob/master/lib/active_resource/validations.rb#L160
@mcmire
Copy link
Collaborator

mcmire commented Sep 13, 2013

PR #352 was merged so I'm closing this.

@mcmire mcmire closed this as completed Sep 13, 2013
@fabianoalmeida
Copy link

Hi @mcmire! I'm facing exactly the same problem. I'm using shoulda-matchers (3.1.2) and Rails 5. I tried with this:

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

in spec/rails_helper.rb file and nothing.

Do you have any suggestion?

@mcmire
Copy link
Collaborator

mcmire commented Nov 2, 2017

@fabianoalmeida What's the error you're getting and what's the backtrace?

@taka41s
Copy link

taka41s commented Nov 25, 2023

same here

runned: it { should validate_presence_of(:warehouse) }

   ArgumentError:
     wrong number of arguments (given 3, expected 1..2)
   # /bundle/ruby/3.2.0/gems/mask_validator-0.2.3/lib/mask_validator.rb:27:in `validate_each'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:96:in `perform_validation'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:89:in `validation_result'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:85:in `validation_error_messages'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:64:in `messages'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:25:in `has_messages?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:55:in `messages_match?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validator.rb:21:in `call'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:38:in `matches?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `each'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `detect'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `first_passing'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:536:in `public_send'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:536:in `run'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:401:in `does_not_match?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:32:in `matches?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validation_matcher.rb:179:in `run_allow_or_disallow_matcher'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validation_matcher.rb:103:in `disallows_value_of'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:258:in `disallows_original_or_typecast_value?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:180:in `block in matches?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:179:in `all?'
   # /bundle/ruby/3.2.0/gems/shoulda-matchers-5.3.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:179:in `matches?'
   # ./spec/models/consumption_limit_spec.rb:19:in `block (2 levels) in <top (required)>'

`

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

Successfully merging a pull request may close this issue.

4 participants