电子邮件已经拍摄,Rails教程第7章

时间:2014-04-14 21:49:31

标签: factory-bot

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

Failures:

1)用户页面个人资料页面      失败/错误:让(: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:in阻止(3级)'

2)用有效信息注册的用户页面应该创建一个用户      失败/错误:期望{click_button submit}。更改(用户,:计数).by(1)        count应该已经被改变了1,但被改为0      './spec/requests/user_pages_spec.rb:43:in'块(4级)in'

我的spec / models / 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

和factories.rb

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

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

也许您的硬编码用户使用相同的电子邮件。要么手动删除用户,要么运行bundle exec rake db:reset,这将消除给你带来麻烦的用户(这将清除你的整个数据库btw所以确保这是你想要做的)。在阅读本教程时,我已经遇到过这个问题几次。