Rails 应用程序和 Cucumber with Factory Girl 中的验证失败错误
我在尝试运行 Devise 用户登录功能时出现以下错误:“验证失败:电子邮件地址已被使用”。
我怀疑问题出在工厂生成的用户中,该用户以某种方式使用相同的电子邮件地址创建了两次。我尝试运行 rake db:test:prepare 来重置测试数据库,不幸的是,没有结果。
我的 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
它可能是什么? 预先感谢您的帮助。
I have the following error: "Validation failed: Email address is already used" while trying to run feature for Devise user signing in.
I suspect the problem is in Factory generated user, which somehow being created twice with the same email address. I tried to run rake db:test:prepare in order to reset test database, unfortunately, with no results.
my 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
Steps, which are failing with validation error are "Then I should be registered and signed in" and "And user signs in". my 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"
my 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
What it can be?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在测试之间清除数据库,主要使用 database_cleaner。请检查手册,因为设置之间的配置有所不同。我们正在使用这个(在
env.rb
中):此外,您应该删除工厂文件中的
rand
指令并使用 FactoryGirl 提供的好东西:You should be clearing the database between tests, mostly using database_cleaner. Check the manual because configuration varies between setups. We are using this one (in
env.rb
):Also, you should remove the
rand
directive in the factories file and use the goodies FactoryGirl provides: