本应失败的 RSPEC 测试已通过
我使用 rspec、rails、guard 和 sorcery 进行身份验证和测试。
我有一个测试正在测试电子邮件的长度。我想拒绝太长的电子邮件。这是我为 spec/models/user_spec.rb 编写的测试
require 'spec_helper'
describe User do
before(:each) do
@attr = { :email => "[email protected]", :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,它会变成红色。提前致谢!
I'm using rspec, rails, guard and sorcery for my authentication and testing.
I have a test that is testing the length of an email. I want to reject emails that are too long. Here is the test I wrote for spec/models/user_spec.rb
require 'spec_helper'
describe User do
before(:each) do
@attr = { :email => "[email protected]", :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
Here is the model validations I have in place:
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
I'm a noob with this stuff, so any help would be greatly appreciated. The test is green right now, it goes red if I change the line to long_email.should be_valid. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@apneadiving 指出的,测试的格式很重要。它不仅使其更易于阅读,而且还显示您尚未测试的地方:
如您所见,重写它做了几件事:
let
而不是before
)如果您写积极的情况也过去了,那么你就真的知道出了问题!
As @apneadiving points out, the format of test is important. It not only makes it easier to read, but also shows up places you haven't tested:
As you can see, rewriting it has done several things:
let
instead ofbefore
)If you write the positive case and that passes too then you really know something is wrong!