【问题标题】:Email Already Taken, Rails Tutorial chapter 7已接收电子邮件,Rails 教程第 7 章
【发布时间】:2014-05-29 01:27:45
【问题描述】:

我在这里不知所措,一切都很顺利,直到 Michael Hartl 的 Rails 教程第 7 章进行到一半。我现在的测试失败了,说电子邮件已经被接收了,并且(我相信是因为第一个错误)它不会保存一个有效的用户来将计数更改为 1。我已经回到第 6 章和基本上将代码复制并粘贴回我的程序中,希望它是嵌套错误或类似错误,但没有这样的运气。这是我得到的失败测试:

Failures:

1) 用户页面个人资料页面 失败/错误:let(:user) { FactoryGirl.create(:user) } ActiveRecord::RecordInvalid: 验证失败:电子邮件已被占用 # ./spec/requests/user_pages_spec.rb:8:in block (3 levels) in <top (required)>' # ./spec/requests/user_pages_spec.rb:9:inblock (3 个级别) in '

2) 使用有效信息注册的用户页面应创建一个用户 失败/错误:期望 { click_button submit }.to change(User, :count).by(1) count 应该被改变了 1,但是被改变了 0 # ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in '

我的规格/模型/user_spec.rb

require 'spec_helper'

describe User do

  before do
  @user = User.new(name: "Example User", email: "user@example.com",
                   password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end


  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        expect(@user).not_to be_valid
      end
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        expect(@user).to be_valid
      end
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "when password is not present" do
    before do
      @user = User.new(name: "Example User", email: "user@example.com",
                     password: " ", password_confirmation: " ")
    end
    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should_not be_valid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by(email: @user.email) }

    describe "with valid password" do
      it { should eq found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not eq user_for_invalid_password }
      specify { expect(user_for_invalid_password).to be_false }
    end
  end
end

user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end

  describe "signup" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end
    end
  end
end

我知道人们在使用 FactoryGirl 时遇到过类似的问题,但通常是在本教程的后面部分,我收集到的内容与写入开发数据库有关,我已确认在我的实例中没有发生这种情况。如果有人有任何帮助、提示或资源,他们现在将不胜感激。

编辑: 添加了 User.rb

class User < ActiveRecord::Base

    before_save { self.email = email.downcase }
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, length: { minimum: 6 }
end

和 factory.rb

FactoryGirl.define do
  factory :user do
    name     "Example Guy"
    email    "guy@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end

谢谢!

【问题讨论】:

标签: factory-bot


【解决方案1】:

如果没有看到您的 User 模型和 factories.rb 文件,很难知道,但我猜您对用户的电子邮件有唯一性限制。类似validates :email, uniqueness: true

另外,我敢打赌你已经在你的用户工厂中指定了一个硬编码的电子邮件地址。

由于这个限制,factory_girl 正在创建一个电子邮件为“foo@bar.com”的用户,然后您尝试创建第二个用户,但使用相同的电子邮件地址,并且验证(正确)阻止您保存这个新用户。

如果是这种情况,您希望使用factory_girl 生成的每个用户都有一个唯一 电子邮件地址,这可以使用sequences 完成。

【讨论】:

  • 用请求的文件更新了原始帖子。唯一性似乎不是我的问题,我今天早些时候尝试了排序,但无济于事,但我会再试一次
【解决方案2】:

也许您有一个硬编码的用户使用相同的电子邮件。手动删除用户或运行bundle exec rake db:reset,这将摆脱给您带来麻烦的用户(顺便说一句,这将清除您的整个数据库,因此请确保这是您想要做的)。我在阅读教程时也遇到过几次这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多