将此自定义验证添加到用户模型

时间:2017-03-27 15:47:13

标签: ruby-on-rails rspec

我有一个用户模型,如果我添加此自定义验证,则我的空rspec测试失败:

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

我运行rspec spec/models/user_spec.rb并得到:

  

/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 救人!”从   /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 阻止保存!'从   /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 交易'   /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'   /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!”从   /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 阻止事务'来自   /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 交易'   /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 创建'来自   /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'来自   /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'   /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'   /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'   /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 从'跑'   /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 来自   /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'   /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 报告来自   /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 从'跑'   /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 调用'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加载'   /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'来自   /Users/blank/.rvm/gems/ruby-2.3.3@myapp/bin/ruby_executable_hooks:15:in   `'

为什么会这样?如果我删除了自定义验证,它运行正常。

我的模特是:

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 个答案:

答案 0 :(得分:2)

更改

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

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