自動テストに問題があります。私のユーザーモデルでは、ユーザー名とメールアドレスは一意です。自動テストを開始すると、すべてが正常に機能します。2回目のラウンドでは、自動テストから、私は
検証に失敗しました:電子メールはすでに取得されています、ユーザー名はすでに取得されています
たった2つのテストでエラーが発生しました。理由がわかりません。factorygirlをシーケンスで使用します。これにより、新しいユーザー名が生成されるたびに生成されます。
これは私の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
動作していない2つのテストは、「一意の値」のコンテキストにあります。他のすべてのテストはエラーなしで機能します。
ありがとう