1

I have a user model, if I add this custom validation my empty rspec test fails:

validate :password_complexity 

def password_complexity
    if !password.blank?
        errors.add(:password, "must contain a upper case character") if password.match(/[A-Z]/)
    end
end

Rspec:

require 'rails_helper'

RSpec.describe User, type: :model do

end

I run rspec spec/models/user_spec.rb and I get:

/Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/validations.rb:79:in raise_record_invalid': Validation failed: Password must contain a upper case character, Password must contain a lower case character (ActiveRecord::RecordInvalid) from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/validations.rb:43:in save!' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/attribute_methods/dirty.rb:29:in save!' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in block in save!' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:351:in block in with_transaction_returning_status' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in transaction' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in transaction' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:in with_transaction_returning_status' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in save!' from /Users/blank/dev/gitserver/myapp/myappweb/app/models/user.rb:74:in create_from_signup!' from /Users/blank/dev/gitserver/myapp/myappweb/app/services/account_service.rb:16:in block in create' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in block in transaction' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in within_new_transaction' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in transaction' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in transaction' from /Users/blank/dev/gitserver/myapp/myappweb/app/services/account_service.rb:11:in create' from /Users/blank/dev/gitserver/myapp/myappweb/db/seeds.rb:172:in <top (required)>' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in load' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in block in load' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in load_dependency' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in load' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.5/lib/rails/engine.rb:547:in load_seed' from /Users/blank/dev/gitserver/myapp/myappweb/spec/spec_helper.rb:24:in block (2 levels) in <top (required)>' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/example.rb:424:in instance_exec' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/example.rb:424:in instance_exec' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/hooks.rb:357:in run' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1724:in block in run_hooks_with' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1724:in each' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1724:in run_hooks_with' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1679:in with_suite_hooks' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:114:in block in run_specs' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/reporter.rb:77:in report' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:113:in run_specs' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:89:in run' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in run' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in invoke' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/gems/rspec-core-3.4.1/exe/rspec:4:in <top (required)>' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/bin/rspec:22:inload' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/bin/rspec:22:in <main>' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/bin/ruby_executable_hooks:15:in eval' from /Users/blank/.rvm/gems/ruby-2.3.3@myapp/bin/ruby_executable_hooks:15:in `'

Why is this happening? If I remove that custom validation, it runs fine.

My model is:

class User < ActiveRecord::Base
    has_secure_password 

  belongs_to :account

    validates :email, uniqueness: { scope: :account_id }
    validates_presence_of :email
    validates_presence_of :password, :password_confirmation, on: :create

    validates_length_of :password, :minimum => 8, :maximum => 32, :allow_blank => false
    #validate :password_complexity 


    def password_complexity
        if !password.blank?
            errors.add(:password, "must contain a upper case character") unless password.match(/[A-Z]/)
        end
    end
end

1 Answer 1

2

Change

errors.add(:password, "must contain a upper case character") if password.match(/[A-Z]/)

To

errors.add(:password, "must contain a upper case character") unless password.match(/[A-Z]/)
Sign up to request clarification or add additional context in comments.

4 Comments

I see this /active_record/validations.rb:79:in `raise_record_invalid': Validation failed: Password must contain a number (ActiveRecord::RecordInvalid)
Post your full model above please
When I comment out validate :password_complexity the error goes away.
What happens if you use password =~ /[A-Z]/ instead of match?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.