Rails 应用程序和 Cucumber with Factory Girl 中的验证失败错误

发布于 2025-01-02 23:53:12 字数 2193 浏览 1 评论 0原文

我在尝试运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2025-01-09 23:53:12

您应该在测试之间清除数据库,主要使用 database_cleaner。请检查手册,因为设置之间的配置有所不同。我们正在使用这个(在 env.rb 中):

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

此外,您应该删除工厂文件中的 rand 指令并使用 FactoryGirl 提供的好东西:

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

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):

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

Also, you should remove the rand directive in the factories file and use the goodies FactoryGirl provides:

 u.sequence(:email){|n| "user_#{n}@example.com" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文