Reputation: 267010
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:insave!' 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:inblock 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:intransaction' 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:insave!' 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:inblock 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:inwithin_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:intransaction' 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:inblock 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:inload' 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:inblock (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:ininstance_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:inblock 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:inrun_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:inblock 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:inrun_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:inrun' 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:in
load' 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
Upvotes: 1
Views: 192
Reputation: 26071
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]/)
Upvotes: 2