Ruby on rails 工厂女孩设计用户工厂无效-不';t通过RSpec测试

Ruby on rails 工厂女孩设计用户工厂无效-不';t通过RSpec测试,ruby-on-rails,rspec,devise,factory-bot,Ruby On Rails,Rspec,Devise,Factory Bot,我是编程新手,已经学习RubyonRails大约11周了 当尝试测试我的用户工厂进行验证时,我得到 1) User has a valid factory Failure/Error: expect(@user.valid?).to eq(true) expected: true got: false (compared using ==) 我使用Desive作为我的用户模型,FactoryGirl作为我的工厂 这是我的用户

我是编程新手,已经学习RubyonRails大约11周了

当尝试测试我的用户工厂进行验证时,我得到

  1) User has a valid factory
     Failure/Error: expect(@user.valid?).to eq(true)

       expected: true
            got: false

       (compared using ==)
我使用Desive作为我的用户模型,FactoryGirl作为我的工厂

这是我的用户工厂:

FactoryGirl.define do 
  factory :user do
    name "John Fahey"
    sequence(:email, 100) { |n| "person#{n}@example.com" }
    password "password"
    password_confirmation "password"
    confirmed_at Time.now
  end
end
…这是我的规格

require 'rails_helper'

  describe User do 
    before do
      @user = build(:user)
    end

  it "has a valid factory" do
   expect(@user.valid?).to eq(true)
   end 
end
我一直在努力让这个规范通过一段时间了。有一段时间,我收到了“电子邮件已被接收”的错误,我已经克服了这一错误。我甚至让规范通过了一次,但我使用的是现在不推荐的“应该是”语法。当我使用正确的“:expect”语法时,我会得到这个错误。有人知道我做错了什么吗

这是我的模型

class User < ActiveRecord::Base

  #== schema information. 
  # create_table "users", force: true do |t|
  #   t.string   "name"
  #   t.string   "email",                  default: "", null: false
  #   t.string   "encrypted_password",     default: "", null: false
  #   t.string   "reset_password_token"
  #   t.datetime "reset_password_sent_at"
  #   t.datetime "remember_created_at"
  #   t.integer  "sign_in_count",          default: 0,  null: false
  #   t.datetime "current_sign_in_at"
  #   t.datetime "last_sign_in_at"
  #   t.string   "current_sign_in_ip"
  #   t.string   "last_sign_in_ip"
  #   t.string   "confirmation_token"
  #   t.datetime "confirmed_at"
  #   t.datetime "confirmation_sent_at"
  #   t.string   "unconfirmed_email"
  #   t.datetime "created_at"
  #   t.datetime "updated_at"
  # end

  # add_index "users", ["email"], name: "index_users_on_email", unique: true
  # add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true


  has_one :list
  has_many :items, through: :list
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
end
这里有一些有趣的事情。为了确保这一点,我做了一个rakedb:migrate和一个rakedb:test:prepare。考试通过了。然后我又做了一次精确的测试,结果失败了

vagrant@rails-dev-box:~/code/blocitoff$ rake db:migrate
vagrant@rails-dev-box:~/code/blocitoff$ rake db:migrate
vagrant@rails-dev-box:~/code/blocitoff$ rake db:test:prepare
vagrant@rails-dev-box:~/code/blocitoff$ rspec spec/models/user_spec.rb
#<ActiveModel::Errors:0xbdf0740>
.

Finished in 0.1764 seconds (files took 8.96 seconds to load)
1 example, 0 failures
vagrant@rails-dev-box:~/code/blocitoff$ rspec spec/models/user_spec.rb
#<ActiveModel::Errors:0xa97066c>
F

Failures:

  1) User has a valid factory
     Failure/Error: expect(@user.valid?).to eq(true)

       expected: true
            got: false

       (compared using ==)
vagrant@rails-开发框:~/code/blocitoff$rake db:migrate
vagrant@rails-开发框:~/code/blocitoff$rake db:migrate
vagrant@rails-开发框:~/code/blocitoff$rake db:test:prepare
vagrant@rails-开发框:~/code/blocitoff$rspec spec/models/user\u spec.rb
#
.
完成时间为0.1764秒(文件加载时间为8.96秒)
1例,0次失败
vagrant@rails-开发框:~/code/blocitoff$rspec spec/models/user\u spec.rb
#
F
失败:
1) 用户具有有效的工厂
失败/错误:预期(@user.valid?)为eq(true)
预期:正确
得了:错
(使用==进行比较)

您的
email
属性必须是唯一的,并且您没有在测试之间清理数据库,因此在第二次和后续执行测试时会出错。请参阅以了解RSpec中事务的使用。

仅打包Peter所说的内容,我可以通过将此设置添加到spec_helper.rb中来解决我的问题

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

请发布带有验证的
app/models/user.rb
。调试它在“具有有效工厂”内投诉的原因,例如:断言前的:
@user.errors
。好的,我已更新了我的问题,以包括我的模型和架构信息。@EricLowber尝试调试,如@blemupp,put
@user.save;放置(@user.errors)
并查看
log/test.log
中的任何事件。Desive拥有自己的验证,看起来其中一些无法通过测试。好的。当我尝试调试和创建一个用户实例时,该实例甚至没有用户id。rails不应该自动生成它吗?问题是,我有一个数据库清理器。但是,我的数据库清理器文件中有错误的语法。没有使用DatabaseCleaner。取而代之的是databaseCleaner。我很好奇:您是否在
databaseCleaner
引用中生成了一个可见错误?没有。我只是准备说我已经有了一个数据库清洁器。然后我检查了一下文件,确保所有的信号都正常,结果发现没有。顺便说一句,谢谢你在这方面的帮助。
vagrant@rails-dev-box:~/code/blocitoff$ rake db:migrate
vagrant@rails-dev-box:~/code/blocitoff$ rake db:migrate
vagrant@rails-dev-box:~/code/blocitoff$ rake db:test:prepare
vagrant@rails-dev-box:~/code/blocitoff$ rspec spec/models/user_spec.rb
#<ActiveModel::Errors:0xbdf0740>
.

Finished in 0.1764 seconds (files took 8.96 seconds to load)
1 example, 0 failures
vagrant@rails-dev-box:~/code/blocitoff$ rspec spec/models/user_spec.rb
#<ActiveModel::Errors:0xa97066c>
F

Failures:

  1) User has a valid factory
     Failure/Error: expect(@user.valid?).to eq(true)

       expected: true
            got: false

       (compared using ==)
RSpec.configure do |config|
  config.use_transactional_fixtures = true
end