2012-02-06 63 views
1

我有以下錯誤:「驗證失敗:電子郵件地址已被使用。」而試圖在設計用戶簽約運行功能驗證在Rails應用程序和黃瓜工廠女孩失敗的錯誤

我懷疑問題在工廠生成的用戶,這在某種程度上用相同的電子郵件地址創建兩次。我試圖運行rake db:test:準備爲了重置測試數據庫,不幸的是,沒有結果。

user_factory.rb

Factory.define :user do |user| 
    user.email "user_#{rand(1000).to_s}@example.com" 
    user.password "password" 
    user.confirmed_at nil 
end 

步驟,這是與驗證錯誤失敗是「那我應該註冊,並簽訂了」「在與用戶的跡象」。我signing_in.feature

Feature: Signing in 
    In order to use the site 
    As a user 
    I want to be able to sign in 

    Scenario: Signing in via confirmation 
    Given there is an unconfirmed user 
    And I open the email with subject "Confirmation instructions" 
    And they click the first link in the email 
    Then I should be registered and signed in 

    Scenario: Signing in via form 
    Given there is a confirmed user 
    And I am on the homepage 
    And user signs in 
    Then I should see "Signed in successfully" 

user_steps.rb

def unconfirmed_user 
    @unconfirmed_user = Factory(:user) 
end 


def sign_up user 
    visit '/users/sign_up' 
    fill_in('Email', :with => "[email protected]") 
    fill_in('Password', :with => user.password) 
    fill_in('Password confirmation', :with => user.password) 
    click_button('Sign up') 
end 

def sign_in user 
    visit 'users/sign_in' 
    fill_in('Email', :with => user.email) 
    fill_in('Password', :with => user.password) 
    click_button('Sign in') 
end 

When /^I'm signing up$/ do 
    sign_up unconfirmed_user 
end 

Given /^there is an unconfirmed user$/ do 
    unconfirmed_user 
end 

Given /^there is a confirmed user$/ do 
    unconfirmed_user.confirm! 
end 

Then /^I should be registered and signed in$/ do 
    page.should have_content("Signed in as #{unconfirmed_user.email}") 
end 

Given /^user signs in$/ do 
    sign_in unconfirmed_user.confirm! 
end 

When /^I open the email with subject "([^"]*?)"$/ do |subject| 
    open_email(@unconfirmed_user.email, :with_subject => subject) 
end 

它可以是什麼? 在此先感謝您的幫助。

回答

0

您應該在測試之間清除數據庫,主要使用database_cleaner。檢查手冊,因爲設置之間的配置不同。我們使用這個(在env.rb):

DatabaseCleaner.strategy = :truncation 
Before do 
    DatabaseCleaner.clean 
end 

此外,您應該刪除rand指令在工廠文件,並使用FactoryGirl提供超值服務:

u.sequence(:email){|n| "user_#{n}@example.com" } 
+0

而且,你不應該被灌在電子郵件領域與user.email在註冊方法呢? – AlistairH 2012-02-06 15:52:56