2012-02-06 61 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