作者:swa乄ycat曼颜 | 来源:互联网 | 2023-09-01 10:38
篇首语:本文由编程笔记#小编为大家整理,主要介绍了ruby 简单易用的用户验证相关的知识,希望对你有一定的参考价值。
require 'spec_helper'
describe User do
describe "Validations" do
context "on a new user" do
it "should not be valid without a password" do
user = User.new password: nil, password_confirmation: nil
user.should_not be_valid
end
it "should be not be valid with a short password" do
user = User.new password: 'short', password_confirmation: 'short'
user.should_not be_valid
end
it "should not be valid with a confirmation mismatch" do
user = User.new password: 'short', password_confirmation: 'long'
user.should_not be_valid
end
end
context "on an existing user" do
let(:user) do
u = User.create password: 'password', password_confirmation: 'password'
User.find u.id
end
it "should be valid with no changes" do
user.should be_valid
end
it "should not be valid with an empty password" do
user.password = user.password_cOnfirmation= ""
user.should_not be_valid
end
it "should be valid with a new (valid) password" do
user.password = user.password_cOnfirmation= "new password"
user.should be_valid
end
end
end
end
class User has_secure_password
validates :password, length: { minimum: 8 }, allow_nil: true
end
Failures:
1) User Validations on an existing user should be valid with no changes
Failure/Error: user.should be_valid
expected # to be valid, but got errors: Password can't be blank, Password is too short (minimum is 8 characters)
# ./spec/models/user_spec.rb:29:in `block (4 levels) in '
class User attr_accessible :name, :password, :password_confirmation
has_secure_password
validates :password, presence: true, confirmation: true, length: { minimum: 8 }
end