【问题标题】:integration_sign_up in railstutorial chapter 9 exercises not workingrailstutorial 第 9 章练习中的 integration_sign_up 不起作用
【发布时间】:2012-01-13 17:47:30
【问题描述】:

我正在学习 Rails 教程中的 rais:http://ruby.railstutorial.org/chapters/sign-in-sign-out#fnref:9.14

我正在练习。任务是在规范助手中创建一个登录方法,以便它可以在集成测试中使用。他已经给出了代码:

def integration_sign_in(user)
    visit signin_path
    fill_in :email,    :with => user.email
    fill_in :password, :with => user.password
    click_button
  end

所以,在我的 layout_links_spec.rb 集成测试中,我打算使用它。

before(:each) do
      @user = Factory(:user)
      visit signin_path
      fill_in :email,    :with => @user.email
      fill_in :password, :with => @user.password
      click_button
      # integration_sign_in(Factory(:user))
    end

我将所有内容都注释掉并使用integration_sign_in(Factory(:user))。我得到的错误是

ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken

但是,如果我使用看起来与 integration_sign_in 函数非常相似的原始版本,则测试通过。有人可以解释一下吗?

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec railstutorial.org


    【解决方案1】:

    听起来您的 :user 工厂每次创建用户时都会返回相同的电子邮件地址。因为你有 before(:each) ,所以它会生成多个用户并将其填充到数据库中,并且他们都将拥有相同的电子邮件。

    如果您发布您的工厂,我们可能会对其进行调整以使用序列或找到替代解决方案。

    【讨论】:

    • 啊。完美的解释我的 factory.rb 是这样的:Factory.define :user do |user| user.name "Michael Hartl" user.email "mhartl@example.com" user.password "foobar" user.password_confirmation "foobar" end
    • 您将 user.email "mhartl@example.com" 更改为 f.sequence(:email) { |n| "foo#{n}@example.com" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多