【发布时间】:2011-06-03 17:09:05
【问题描述】:
我对自动测试有疑问。在我的用户模型中,用户名和电子邮件地址是唯一的。当我开始自动测试时,一切正常。在第二轮,通过自动测试,我有一个
验证失败:电子邮件已被占用,用户名已被占用
只有两次测试出错。我不明白为什么,我使用 factorygirl 和 sequenze,这应该每次生成一个新的用户名。
这是我的 rspec2 文件:
describe User do
specify { Factory.build(:user).should be_valid }
context "unique values" do
before :all do
@user = Factory.create(:user)
end
it "should have an unique email address" do
Factory.build(:user, :email => @user.email).should_not be_valid
end
it "should have an unique username" do
Factory.build(:user, :username => @user.username).should_not be_valid
end
end
context "required attributes" do
it "should be invalid without an email address" do
Factory.build(:user, :email => nil).should_not be_valid
end
it "should be invalid without an username" do
Factory.build(:user, :username => nil).should_not be_valid
end
it "should be invalid without an password" do
Factory.build(:user, :password => nil).should_not be_valid
end
it "should be invalid without an address" do
Factory.build(:user, :address => nil).should_not be_valid
end
end
end
工厂:
Factory.sequence :username do |n|
"Outfit#{n}er"
end
Factory.sequence :email do |n|
"user#{n}@example.com"
end
Factory.define :user do |u|
u.username { Factory.next :username }
u.email { Factory.next :email }
u.password 'secret'
u.phone_number '02214565854'
u.association :address, :factory => :address
end
Factory.define :confirmed_user, :parent => :user do |u|
u.after_build { |user| user.confirm! }
end
唯一两个不起作用的测试是在“唯一值”上下文中。所有其他测试均正常运行。
谢谢
【问题讨论】:
标签: ruby-on-rails ruby factory-bot rspec2 autotest