【发布时间】:2011-09-13 20:02:17
【问题描述】:
我正在使用 rspec、rails、guard 和 sorcery 进行身份验证和测试。
我有一个测试电子邮件长度的测试。我想拒绝太长的电子邮件。这是我为 spec/models/user_spec.rb 编写的测试
require 'spec_helper'
describe User do
before(:each) do
@attr = { :email => "testuser@example.com", :password => "password", :password_confirmation => "password" }
end
it "should reject emails that are too long" do
long_email = "a" * 101 + "gmail.com"
long_email = User.new (@attr.merge(:email => long_email))
long_email.should_not be_valid
end
这是我的模型验证:
class User < ActiveRecord::Base
authenticates_with_sorcery!
attr_accessible :email, :password, :password_confirmation
validates_presence_of :password, :on => :create
validates :password, :confirmation => true,
:length => { :within => 6..100 }
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => {:case_sensitive => false},
:length => { :within => 5..100 }
end
我是这个东西的菜鸟,所以任何帮助将不胜感激。测试现在是绿色的,如果我将行更改为 long_email.should be_valid,它会变成红色。提前致谢!
【问题讨论】:
-
听起来您的应用程序的行为完全符合您的预期。如果您的测试是 long_email.should_not be_valid,并且您的测试通过了,那似乎就是您想要的。如果没有,您能否更详细地解释您的问题?
-
您的电子邮件不是太长,它是 60 个字符,并且您接受
标签: ruby-on-rails rspec