Sag*_*ual 0 ruby rspec ruby-on-rails railstutorial.org
通过Michael Hartl的Ruby on Rails 优秀教程,我的工作方式.我正在创建一个检查重复电子邮件地址的测试,我对他使用upcase,downcase和case-insensitive检查有点困惑.
测试(代码清单6.17)如下所示:
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com")
end
.
.
.
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
end
Run Code Online (Sandbox Code Playgroud)
请注意拨打电话upcase.一切都很好.但在他的有效性检验(6.18),他将区分大小写关闭.
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
Run Code Online (Sandbox Code Playgroud)
什么?如果他要进行不区分大小写的验证,他为什么要将副本转换为大写?
最后,在6.20中,他设置了一个before_save块,将新用户的电子邮件转换为小写.
before_save { self.email = email.downcase }
Run Code Online (Sandbox Code Playgroud)
这很有道理,因为你想在数据库中使用小写.但我很困惑为什么他在测试中使用大写,因为保存将把电子邮件地址转换为小写无论如何.我错过了一些明显的东西吗
| 归档时间: |
|
| 查看次数: |
1208 次 |
| 最近记录: |