热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ruby简单易用的用户验证

篇首语:本文由编程笔记#小编为大家整理,主要介绍了ruby简单易用的用户验证相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了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


推荐阅读
author-avatar
swa乄ycat曼颜
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有